标签:efi nbsp rgb nec line 需要 color pre unsigned
之前有写过一个color对象转16进制的,后来在项目中发现调整亮度,还是需要获取当前颜色的存储值。。
一个是当前设备的颜色,一个是颜色拾取图上的颜色,存下来后转为color对象再分解成饱和度,亮度等等……
下面奉上16进制转color对象的方法
1 //把16进制转为UIcolor对象的代码 2 - (UIColor *) colorWithHexString: (NSString *) stringToConvert 3 { 4 NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]uppercaseString];//字符串处理 5 //例子,stringToConvert #ffffff 6 if ([cString length] <6) 7 return [UIColor clearColor];//如果非十六进制,返回白色 8 if ([cString hasPrefix:@"#"]) 9 cString = [cString substringFromIndex:1];//去掉头 10 if ([cString length] !=6)//去头非十六进制,返回白色 11 return [UIColor clearColor]; 12 //分别取RGB的值 13 NSRange range; 14 range.location =0; 15 range.length =2; 16 NSString *rString = [cString substringWithRange:range]; 17 range.location =2; 18 NSString *gString = [cString substringWithRange:range]; 19 range.location =4; 20 NSString *bString = [cString substringWithRange:range]; 21 unsigned int r, g, b; 22 //NSScanner把扫描出的制定的字符串转换成Int类型 23 [[NSScanner scannerWithString:rString] scanHexInt:&r]; 24 [[NSScanner scannerWithString:gString] scanHexInt:&g]; 25 [[NSScanner scannerWithString:bString] scanHexInt:&b]; 26 //转换为UIColor 27 return [UIColor colorWithRed:((float) r /255.0f) 28 green:((float) g /255.0f) 29 blue:((float) b /255.0f) 30 alpha:1.0f]; 31 32 }
标签:efi nbsp rgb nec line 需要 color pre unsigned
原文地址:https://www.cnblogs.com/kc1995/p/13636959.html