• UIColor、CIColor 和 CGColor 出现在不同的类库里面,其实就是颜色存储方式不同而已,比如 999 可以用 10 进制、2 进制、16 进制等存储。三者之间都是能够方便转换的,特别是很多构造函数。

0、极速宏

/** 颜色生成 */
#define CHColor_A(r,g,b,a) [UIColor colorWithRed:r/255.f green:g/255.f blue:b/255.f alpha:a]
/** 颜色生成 */
#define CHColor(r,g,b) CHColor_A(r,g,b,1.0) /** 十六进制颜色 -> 0x123456 */
#define CHRGB16HEX(hex_0x) [UIColor colorWithRed:((float)((hex_0x & 0xFF0000) >> 16))/255.0 green:((float)((hex_0x & 0xFF00) >> 8))/255.0 blue:((float)(hex_0x & 0xFF))/255.0 alpha:1.0] #define CHRGB16HEX_A(hex_0x, a) [UIColor colorWithRed:((float)((hex_0x & 0xFF0000) >> 16))/255.0 green:((float)((hex_0x & 0xFF00) >> 8))/255.0 blue:((float)(hex_0x & 0xFF))/255.0 alpha:a] /** 设置随机颜色 */
#define CHRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0] /** 黑色 */
#define Black_Color [UIColor blackColor]
/** 深灰色 */
#define DarkGray_Color [UIColor darkGrayColor]
/** 浅灰色 */
#define LightGray_Color [UIColor lightGrayColor]
/** 白色 */
#define White_Color [UIColor whiteColor]
/** 灰色 */
#define Gray_Color [UIColor grayColor]
/** 红色 */
#define Red_Color [UIColor redColor]
/** 绿色 */
#define Green_Color [UIColor greenColor]
/** 蓝色 */
#define Blue_Color [UIColor blueColor]
/** 蓝绿色 */
#define Cyan_Color [UIColor cyanColor]
/** 黄色 */
#define Yellow_Color [UIColor yellowColor]
/** 品红色 */
#define Magenta_Color [UIColor magentaColor]
/** 橘黄色 */
#define Orange_Color [UIColor orangeColor]
/** 紫色 */
#define Purple_Color [UIColor purpleColor]
/** 棕色 */
#define Brown_Color [UIColor brownColor]
/** 透明色 */
#define Clear_Color [UIColor clearColor]

1、UIColor 的创建

  • 1.1 使用系统定义好的颜色创建

UIColor *color1 = [UIColor blueColor];
// 附带设置颜色的透明度
UIColor *color1 = [[UIColor blackColor] colorWithAlphaComponent:0.5];
  • 1.2 由白色透明度创建

// alpha:透明度,1 不透明
UIColor *color2 = [UIColor colorWithWhite:1 alpha:0.5];
  • 1.3 由三原色创建

// arc4random()%256/255.0 获取小于等于 1 大于 0 的随机数字
CGFloat red = arc4random()%256/255.0;
CGFloat green = arc4random()%256/255.0;
CGFloat blue = arc4random()%256/255.0; // Red,green,blue 值的范围是 0 ~ 1,alpha:透明度,1 不透明
UIColor *color3 = [UIColor colorWithRed:red green:green blue:blue alpha:1];
  • 1.4 由图片创建

UIColor *color4 = [UIColor colorWithPatternImage:[UIImage imageNamed:@"13"]];
  • 1.5 由 16 进制字符串创建

NSString *colorString = @"#FF0000";

// 十六进制数字字符串转十进制数字
NSString *s1 = [colorString substringWithRange:NSMakeRange(1, 2)];
unsigned long c1 = strtoul([s1 UTF8String], 0, 16); NSString *s2 = [colorString substringWithRange:NSMakeRange(3, 2)];
unsigned long c2 = strtoul([s2 UTF8String], 0, 16); NSString *s3 = [colorString substringWithRange:NSMakeRange(5, 2)];
unsigned long c3 = strtoul([s3 UTF8String], 0, 16); // Red,green,blue 值的范围是 0 ~ 1,alpha:透明度,1 不透明
UIColor *color5 = [UIColor colorWithRed:c1/255.0 green:c2/255.0 blue:c3/255.0 alpha:1];

2、UIColor 与 CIColor 和 CGColor 的相互转换

// UIColor 转 CGColor
CGColorRef color = [[UIColor greenColor] CGColor]; // CGColor 转 UIColor
UIColor *CG_To_UIColor = [UIColor colorWithCGColor:self.outerCircleV.layer.borderColor];

3、由十六进制颜色值创建 RGB 颜色值

  • 适用于 0Xc83c23、#c83c23、c83c23 格式的十六进制颜色值。
/// 由十六进制颜色值创建 RGB 颜色值,带透明度设置
+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha { NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; // String should be 6 or 8 characters
if ([cString length] < 6) {
return [UIColor clearColor];
} // strip "0X" or "#" if it appears
if ([cString hasPrefix:@"0X"])
cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"])
cString = [cString substringFromIndex:1];
if ([cString length] != 6)
return [UIColor clearColor]; // Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2; // r、g、b
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range]; // Scan values
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];
} /// 由十六进制颜色值创建 RGB 颜色值
+ (UIColor *)colorWithHexString:(NSString *)color { return [UIColor colorWithHexString:color alpha:1.0f];
} // 0X 前缀格式
UIColor *color = [UIColor colorWithHexString:@"0Xc83c23"];
UIColor *color = [UIColor colorWithHexString:@"0Xc83c23" alpha:0.5]; // # 前缀格式
UIColor *color = [UIColor colorWithHexString:@"#c83c23"];
UIColor *color = [UIColor colorWithHexString:@"#c83c23" alpha:0.5]; // 无前缀格式
UIColor *color = [UIColor colorWithHexString:@"c83c23"];
UIColor *color = [UIColor colorWithHexString:@"c83c23" alpha:0.5];

4、其他

05-11 20:48