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
- JavaScript RGB轉換成16進位制顏色JavaScript
- jQuery顏色值轉換為十六進位制形式jQuery
- JavaScript rgb與十六進位制格式轉換JavaScript
- [顏色進位制轉換]js實現rgb和hex的相互轉換JS
- 十六進位制顏色值簡寫
- JavaScript 生成十六進位制顏色值JavaScript
- Qt進位制轉換(十進位制轉十六進位制)QT
- 正則提取十六進位制顏色值
- 【譯】CSS 十六進位制顏色揭祕CSS
- 進位制數轉換方法(八/十六/十)
- 十六進位制顏色透明度對照表
- 進位制之間的轉換之“十六進位制 轉 十進位制 轉 二進位制 方案”
- 二進位制,八進位制,十進位制,十六進位制的相互轉換
- java中二進位制、八進位制、十進位制、十六進位制的轉換Java
- 二進位制,八進位制,十進位制,十六進位制之間的轉換
- 十進位制轉十六進位制
- 【進位制轉換】二進位制、十六進位制、十進位制、八進位制對應關係
- JAVA 二進位制,八進位制,十六進位制,十進位制間進行相互轉換Java
- 計算機基礎進位制轉換(二進位制、八進位制、十進位制、十六進位制)計算機
- [C/C++11]_[初級]_[如何轉換帶井號的#十六進位制顏色字串到數值]C++字串
- 一看就懂二進位制、八進位制、十六進位制數轉換十進位制
- [計算機基礎] 計算機進位制轉換:二進位制、八進位制、十進位制、十六進位制計算機
- Go語言實現十進位制轉換成二、八、十六進位制Go
- 二進位制、十進位制與十六進位制相互轉化
- leedcode-數字轉換為十六進位制數
- 整數轉化成八進位制、十六進位制、二進位制,以及轉回
- Python實現"數字轉換為十六進位制"的兩種方法
- 遞迴函式實現十進位制正整數轉換為二進位制,八進位制,十六進位制遞迴函式
- C++資料格式化5 - uint轉換成十六進位制字串&二進位制的data列印成十六進位制字串C++UI字串
- 進位制轉換
- python--顏色的RGB轉BGR(opencv)PythonOpenCV
- Python處理十六進位制與二進位制轉換的問題——binascii自帶庫PythonASCII
- 顏色轉換
- 【python】進位制轉換Python
- 進位制的轉換
- JavaScript 進位制轉換JavaScript
- 牛客網測試題 把十六進位制數字轉換為十進位制數字