iOS 常用RGB十六進位制顏色轉換方法
iOS 常用RGB十六進位制顏色轉換方法
需求
正常美術出圖都是給你十六進位制編碼,然後讓我們在Xcode中,對顏色進行賦值,由於iOS的UIColor並未提供方法直接使用十六進位制編碼對設定顏色的場景進行賦值,所以我們需要造個小輪子。
實現方式
通過分類的方法實現:
UIColor+Addition.h
@interface UIColor (Addition)
//設定RGB顏色
+ (UIColor *)red:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha;
//將顏色轉換成RGB
+ (NSArray *)convertColorToRGB:(UIColor *)color;
//設定十六進位制顏色
+ (UIColor *)colorWithHex:(NSInteger)hex;
+ (UIColor *)colorWithHexString:(NSString *)hexString;
+ (UIColor *)colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha
@end
UIColor+Addition.m
#import "UIColor+Addition.h"
@implementation UIColor (Addition)
+ (UIColor *)red:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha {
return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha];
}
+ (NSArray *)convertColorToRGB:(UIColor *)color {
NSInteger numComponents = CGColorGetNumberOfComponents(color.CGColor);
NSArray *array = nil;
if (numComponents == 4) {
const CGFloat *components = CGColorGetComponents(color.CGColor);
array = @[@((int)(components[0] * 255)),
@((int)(components[1] * 255)),
@((int)(components[2] * 255))];
}
return array;
}
+ (UIColor *)colorWithHex:(NSInteger)hex {
return [UIColor colorWithRed:((float)((hex & 0xff0000) >> 16))/255.0 green:((float)((hex & 0x00ff00) >> 8))/255.0 blue:((float)(hex & 0x0000ff))/255.0 alpha:1.0];
}
+ (UIColor *)colorWithHexString:(NSString *)hexString
{
NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
CGFloat alpha, red, blue, green;
switch ([colorString length]) {
case 3: // #RGB
alpha = 1.0f;
red = [self colorComponentFrom: colorString start: 0 length: 1];
green = [self colorComponentFrom: colorString start: 1 length: 1];
blue = [self colorComponentFrom: colorString start: 2 length: 1];
break;
case 4: // #ARGB
alpha = [self colorComponentFrom: colorString start: 0 length: 1];
red = [self colorComponentFrom: colorString start: 1 length: 1];
green = [self colorComponentFrom: colorString start: 2 length: 1];
blue = [self colorComponentFrom: colorString start: 3 length: 1];
break;
case 6: // #RRGGBB
alpha = 1.0f;
red = [self colorComponentFrom: colorString start: 0 length: 2];
green = [self colorComponentFrom: colorString start: 2 length: 2];
blue = [self colorComponentFrom: colorString start: 4 length: 2];
break;
case 8: // #AARRGGBB
alpha = [self colorComponentFrom: colorString start: 0 length: 2];
red = [self colorComponentFrom: colorString start: 2 length: 2];
green = [self colorComponentFrom: colorString start: 4 length: 2];
blue = [self colorComponentFrom: colorString start: 6 length: 2];
break;
default:
blue=0;
green=0;
red=0;
alpha=0;
break;
}
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}
+ (CGFloat)colorComponentFrom:(NSString *)string start:(NSUInteger)start length:(NSUInteger) length
{
NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
unsigned hexComponent;
[[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
return hexComponent / 255.0;
}
+ (UIColor *)colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha
{
NSString *cString = [[hexString stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
if ([cString length] < 6) {
return [UIColor whiteColor];
}
if ([cString hasPrefix:@"#"]) {
cString = [cString substringFromIndex:1];
}
if ([cString length] != 6) {
return [UIColor whiteColor];
}
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r/255.0f)
green:((float) g/255.0f)
blue:((float) b/255.0f)
alpha:alpha];
}
思考與總結:
需要記住的是:
純黑是 #000000
純白是 #FFFFFF
相關文章
- JavaScript 十六進位制顏色和RGB顏色值的相互轉換JavaScript
- 十六進位制和RGB顏色格式的相互轉換
- 十六進位制顏色轉換為RGB程式碼例項
- 將十六進位制顏色值轉換為RGB顏色值程式碼例項
- js實現的十六進位制顏色和RGB顏色值的相互轉換JS
- RGB顏色與16進位制顏色的換算方法
- JavaScript RGB轉換成16進位制顏色JavaScript
- php中RGB轉十六進位制、十六進位制轉RGBPHP
- jQuery顏色值轉換為十六進位制形式jQuery
- JavaScript rgb與十六進位制格式轉換JavaScript
- [顏色進位制轉換]js實現rgb和hex的相互轉換JS
- 【進位制轉換】十進位制與十六進位制相互轉換方法
- (轉)【iOS 開發】二進位制、十進位制、十六進位制相互轉換的方法iOS
- 十六進位制顏色值簡寫
- JavaScript 生成十六進位制顏色值JavaScript
- 正則提取十六進位制顏色值
- 【譯】CSS 十六進位制顏色揭祕CSS
- Qt進位制轉換(十進位制轉十六進位制)QT
- 十六進位制轉換為八進位制
- 十六進位制轉換為十進位制
- 十六進位制顏色透明度對照表
- 匹配十六進位制顏色正規表示式
- 進位制數轉換方法(八/十六/十)
- Swift3.0-16進位制顏色轉換UIColorSwiftUI
- 匹配十六進位制顏色值正規表示式
- javascriptRGB顏色轉換到16進位制詳解JavaScript
- 十六進位制顏色字串轉換成Color在Silverlight中的實現字串
- IOS 關於16進位制轉顏色 的設定iOS
- 二進位制,八進位制,十進位制,十六進位制的相互轉換
- 進位制之間的轉換之“十六進位制 轉 十進位制 轉 二進位制 方案”
- 遞迴-進位制轉換器(十六進位制以內)遞迴
- Oracle中十進位制與十六進位制轉換程式Oracle
- java中二進位制、八進位制、十進位制、十六進位制的轉換Java
- 二進位制,八進位制,十進位制,十六進位制之間的轉換
- Python 進位制互相轉換(二進位制、十進位制和十六進位制)Python
- 十進位制轉十六進位制
- 隨機生成十六進位制顏色值程式碼例項隨機
- 【進位制轉換】二進位制、十六進位制、十進位制、八進位制對應關係