复制代码 代码如下: // test init uicolor with CGColor UIColor *color = [UIColor colorWithCGColor:[UIColor whiteColor].CGColor];
// CGColor property is always valid NSLog(@"CGColor from UIColor %@", color.CGColor); // don"t use CIColor property // This property throws an exception if the color object was not initialized with a Core Image color. NSLog(@"CIColor from UIColor %@", color.CIColor); // crush
// color with CGColor, uicolor will just retain it UIColor *color = [UIColor colorWithCGColor:colorCMYK]; NSLog(@"CGColor from UIColor: %@", color.CGColor);
color = [UIColor colorWithCIColor:ciColor]; NSLog(@"color %@", color);
// Core Image converts all color spaces to the Core Image working color // space before it passes the color space to the filter kernel. // kCGColorSpaceDeviceGray ---> kCGColorSpaceDeviceRGB NSLog(@"cicolor from UIColor: %@", color.CIColor); NSLog(@"cicolor"s colorspace: %@", color.CIColor.colorSpace); NSLog(@"color"s CGColor: %@", color.CGColor);
ciColor = [CIColor colorWithCGColor:colorCMYK]; NSLog(@"cicolor: %@", ciColor); // in fact,the color value of CIColor has converted to RGB Colorspace NSLog(@"cicolor colorspace: %@", ciColor.colorSpace);
color = [UIColor colorWithCIColor:ciColor]; NSLog(@"UIColor with CIColor: %@", color);
NSLog(@"cicolor from UIColor: %@", color.CIColor); NSLog(@"cicolor"s colorspace: %@", color.CIColor.colorSpace);
// when UIColor init with CIColor, UIColor"s CGColor will convert other colorspace to kCGColorSpaceDeviceRGB NSLog(@"cgcolor from UIColor: %@", color.CGColor);
复制代码 代码如下: // judge two CGColor is equal if (CGColorEqualToColor([UIColor whiteColor].CGColor, [UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor)) { NSLog(@"The two CGColor is equal!"); } else { NSLog(@"The two CGColor is not equal!"); }
if (CGColorEqualToColor([UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor, [UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor)) { NSLog(@"The two CGColor is equal!"); } else { NSLog(@"The two CGColor is not equal!"); } 例子中第一部分是判断两个白色的UIColor是否相等,虽然都是白色,但是颜色空间是不一样的,通过运行我们可以发现,打印出“The two CGColor is not equal!”。例子的第二部分简单的创建了两个RGB空间的UIColor,运行程序可以看出,这两种颜色是相同的。