uicolor
在开发中美工进行标注都是十六进制的颜色值,很少直接写具体的颜色值,有时出现的具体的颜色值也是已经规定好的十六进制的颜色值;十六进制一般以#开头,也有0x开头的,后面的才是真正的颜色值,一般都是6位数字或者字母或者两者的混合,也有8位的,这种是带有透明度的,8位的前两位就是透明度。
透明度参照表:
0%=FF(不透明) 5%=F2 10%=E5 15%=D8 20%=CC 25%=BF 30%=B2 35%=A5 40%=99 45%=8c 50%=7F
55%=72 60%=66 65%=59 70%=4c 75%=3F 80%=33 85%=21 90%=19 95%=0c 100%=00(全透明)
要想把美工标注的颜色值显示出来必须转成uicolor,我们一般把转换方法封装起来:
//如果hexString是8位则参数alpha失效,使用hexString包含的alpha
+ (UIColor *)colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha {
//去除空格
NSString *cString = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
//把开头截取
if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
//6位或8位(带透明度)
if ([cString length] < 6) {
return nil;
}
//取出透明度、红、绿、蓝
unsigned int a, r, g, b;
NSRange range;
range.location = 0;
range.length = 2;
if (cString.length == 8) {
//a
NSString *aString = [cString substringWithRange:range];
//r
range.location = 2;
NSString *rString = [cString substringWithRange:range];
//g
range.location = 4;
NSString *gString = [cString substringWithRange:range];
//b
range.location = 6;
NSString *bString = [cString substringWithRange:range];
[[NSscanner scannerWithString:aString] scanHexInt:&a];
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:(r / 255.0f) green:(g / 255.0f) blue:(b / 255.0f) alpha:(a / 255.0f)];
} else {
//r
NSString *rString = [cString substringWithRange:range];
//g
range.location = 2;
NSString *gString = [cString substringWithRange:range];
//b
range.location = 4;
NSString *bString = [cString substringWithRange:range];
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:(r / 255.0f) green:(g / 255.0f) blue:(b / 255.0f) alpha:alpha];
}
}
下面的方法是把UIColor转换成十六进制颜色值:
- (NSString *)hexString {
//颜色值个数,rgb和alpha
NSinteger cpts = CGColorGetNumberOfcomponents(self.CGColor);
const CGFloat *components = CGColorGetComponents(self.CGColor);
CGFloat r = components[0];//红色
CGFloat g = components[1];//绿色
CGFloat b = components[2];//蓝色
if (cpts == 4) {
CGFloat a = components[3];//透明度
return [NSString stringWithFormat:@"#%02lX%02lX%02lX%02lX", lroundf(a * 255), lroundf(r * 255), lroundf(g * 255), lroundf(b * 255)];
} else {
return [NSString stringWithFormat:@"#%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255)];
}
}
相关阅读
HTML5之placeholder属性以及如何更改placeholder属性
今天在群里看到群友问了一个这样的问题,就是如何更改placeholder属性中文字的颜色,以前用过这属性,却是没更改过颜色,于是便试了试,中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
最近在做图像处理,需要用各种颜色标记各种类型的目标,链接一个颜色对照表过来,方便查看 RGB颜色对照表 RGB颜色值与十六进制颜色码
引言 博主现在每天都在用sublime书写工作日志,储存为Md格式。在网上看到各种好看的theme,按照网上的更新方法去换主题的
OpenCV实战2——进行颜色检测(HSV颜色空间)和分割的隐
原文地址:https://www.learnopencv.com/invisibility-cloak-using-color-detection-and-segmentation-with-opencv/ 开源代码:https