The difference and connection between UIColor, CGColor and CIColor

One, UIColor

  UIColor is an important class for storing color information in UIKit. A UIColor object contains color and transparency values, and its color space has been optimized for IOS. UIColor contains some class methods to create some of the most common colors, such as white, black, red, transparent, etc. The color spaces of these colors are not the same (white and black are kCGColorSpaceDeviceGray, and the red color space is kCGColorSpaceDeviceRGB).

  In addition, UIColor has two important attributes: one is CGColor and the other is CIColor (added after 5.0). These two attributes can link the three objects of UIColor, CGColor, and CIColor. The conversion between these three objects will be described in detail later.

 

Two, CGColor

  CGColor is mainly used in the CoreGaphics framework. CGColor is actually a structure, and we usually use its reference type CGColorRef when we use CGColor. CGColor is mainly composed of CGColorSapce and Color Components. The same color composition, if the color space is different, the parsed result may be different. This is like when we are processing image data, if we regard the RGBA format as the result of the BGRA format, we can imagine. In Quartz 2D, CGColor is commonly used to set the fill color of the context and set the transparency.

1. How to create a CGColor, the most commonly used function is CGColorCreate, this function has two parameters:

  1) colorspace, specify the color space corresponding to CGColor, Quartz will retain the object, so you can safely release the object after the call.

  2) components, an array of CGFloat, the number of elements in the array is the number of color components contained in the specified color space n, plus the corresponding alpha value. 

  This function should return a newly created CGColorRef, and use the CGColorRelease function to release the object when we no longer use the object.

2. Get the data of CGColor

  When we create it, we pass in two important parameters. When we get the CGColorRef, we can of course get the corresponding ColorSpace and Components.

  1) Get ColorSpace

  We can get the ColorSpace corresponding to the current CGColorRef through the CGColorGetColorSpace function. The function only accepts one parameter that is the CGColorRef of the ColorSpace you want to get. Let's look at a simple example:

CGColorRef cgColor = [UIColor redColor].CGColor;
CGColorSpaceRef colorSpace = CGColorGetColorSpace(cgColor);
NSLog(@"color space: %@", colorSpace);

  2) Get Color Components

  To get the color value corresponding to CGColorRef, we need to use two functions, CGColorGetNumberOfComponents and CGColorGetComponents. Let's first take a look at the function prototypes of the two functions:

Copy code
size_t CGColorGetNumberOfComponents (
   CGColorRef color
);

const CGFloat * CGColorGetComponents (
   CGColorRef color
);
Copy code

  The first function is to get the number of color components contained in CGColorRef, and the second function is to get the array of actual color components. Let’s look at a small example:

NSUInteger num = CGColorGetNumberOfComponents(cgColor);
const CGFloat *colorComponents = CGColorGetComponents(cgColor);
for (int i = 0; i < num; ++i) {
    NSLog(@"color components %d: %f", i, colorComponents[i]);
}

 

Three, CIColor

  CIColor is mainly used with other classes in the Core Image framework, such as CIFilter, CIContext and CIImage. Today we are mainly concerned about the color value part, the range of color value in CIColor is between 0.0-1.0, 0.0 means that the color component is the minimum value, and 1.0 means that the color component is changed to the maximum value. The range of the alpha value is also between 0.0 and 1.0, 0.0 means fully transparent, 1.0 means completely opaque, and the color components of CIColor are usually not multiplied by the alpha value.

  我们可以使用initWithCGColor:函数,通过CGColor创建一个CIColor。其中传入的CGColorRef对象可以使任何任何颜色空间,但是Core Image框架会在传入filter kernel之前把所有的颜色空间转换到core image工作颜色空间。core image工作颜色空间使用三个颜色分量加上一个alpha分量组成(其实就是kCGColorSpaceDeviceRGB),后面的例子中我们验证这一点。

 

四、UIColor,CGColor,CIColor的区别和联系

1、UIColor的两个属性CGColor,CIColor

  UIColor的CGColor总是有效的,不管它是通过CGColor,CIColor,还是其他方法创建的,CGColor属性都总是有效的;但是CIColor属性就不总是有效的,只有当UIColor是通过CIColor创建的时候,他才是有效的,否则访问该属性将会抛出异常,下面照旧来一个小例子:

Copy code
// 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
Copy code

2、UIColor使用CGColor初始化

  当UIColor使用CGColor初始化的时候,所有CGColorRef包含的信息,都会被原封不动的保留,其中就包括Color space,而且通过下面的小例子我们还可以看到如果使用CGColor初始化UIColor的时候,UIColor其实是直接保留了一份这个CGColorRef对象。例子如下:

Copy code
// test kCGColorSpaceDeviceCMYK
CGColorSpaceRef cmykSpace = CGColorSpaceCreateDeviceCMYK();
CGFloat cmykValue[] = {
   
   1, 1, 0, 0, 1};      // blue
CGColorRef colorCMYK = CGColorCreate(cmykSpace, cmykValue);
CGColorSpaceRelease(cmykSpace);
NSLog(@"colorCMYK: %@", colorCMYK);
    
// color with CGColor, uicolor will just retain it
UIColor *color = [UIColor colorWithCGColor:colorCMYK];
NSLog(@"CGColor from UIColor: %@", color.CGColor);
Copy code

3、UIColor使用CIColor初始化

  下面我们讨论一下当使用CIColor来初始化一个UIColor的时候,再去访问UIColor的CGColor属性的时候,我们会发现CGColor的color Space和设置CIColor的color space的是不完全一样的,在这个过程中CIColor会为我们做一个转换。下面我们分别看看使用kCGColorSpaceDeviceGray,kCGColorSpaceDeviceRGB,kCGColorSpaceDeviceCMYK三种颜色空间来初始化一个CIColor的时候,再去使用该CIColor去初始化一个UIColor,然后在去访问其CIColor属,CGColor属性,查看颜色空间并打印颜色信息。

  1) 使用kCGColorSpaceDeviceGray初始化CIColor

  首先看代码:

Copy code
 // test kCGColorSpaceDeviceGray
NSLog(@"CGColor white color:%@", [UIColor whiteColor].CGColor);

CIColor *ciColor = [CIColor colorWithCGColor:[UIColor whiteColor].CGColor];
NSLog(@"cicolor: %@", ciColor);
NSLog(@"cicolor colorspace: %@", ciColor.colorSpace);
    
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);
Copy code

  通过运行程序,我们看出来,如果使用一个kCGColorSpaceDeviceGray的颜色空间的CGColor来初始化CIColor的时候,我们可以看到CIColor的色彩空间一直是kCGColorSpaceDeviceGray,通过访问UIColor的CIColor属性,我们可以看到其颜色空间仍然是kCGColorSpaceDeviceGray,但是当访问UIColor的CGColor属性的时候,通过打印可以发现其色彩空间已经转变成了kCGColorSpaceDeviceRGB空间了,而颜色值也正确的从原来的颜色空间转换到了新的颜色空间。

  2) 使用kCGColorSpaceDeviceRGB初始化CIColor

  同样的我们看代码:

Copy code
 //test kCGColorSpaceDeviceRGB
NSLog(@"CGColor red color:%@", [UIColor redColor].CGColor);
    
CIColor *ciColor = [CIColor colorWithCGColor:[UIColor redColor].CGColor];
NSLog(@"cicolor: %@", ciColor);
NSLog(@"cicolor colorspace: %@", ciColor.colorSpace);
    
UIColor *color = [UIColor colorWithCIColor:ciColor];
NSLog(@"color %@", color);
    
NSLog(@"cicolor from UIColor: %@", color.CIColor);
NSLog(@"cicolor's colorspace: %@", color.CIColor.colorSpace);
NSLog(@"color's CGColor: %@", color.CGColor);
Copy code

  整个过程中CIColor,以及通过UIColor的CGColor和CIColor属性访问到的值,打印出来我们可以发现它们都是kCGColorSpaceDeviceRGB空间的。

4、使用kCGColorSpaceDeviceCMYK初始化CIColor

  下面继续看一段代码:

Copy code
// test kCGColorSpaceDeviceCMYK
CGColorSpaceRef cmykSpace = CGColorSpaceCreateDeviceCMYK();
NSLog(@"Components number: %zu", CGColorSpaceGetNumberOfComponents(cmykSpace));
CGFloat cmykValue[] = {
   
   1, 1, 0, 0, 1};      // blue
CGColorRef colorCMYK = CGColorCreate(cmykSpace, cmykValue);
CGColorSpaceRelease(cmykSpace);
NSLog(@"colorCMYK: %@", colorCMYK);
    
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);
Copy code

  整个过程中,我们通过运行同样可以发现,当我们用一个CMYK颜色空间的CGColor来初始化CIColor的时候,CIColor的颜色空间依然是CMYK,但是颜色值已经转换成RGB的颜色值。当使用该CIColor创建一个UIColor的时候,我们再通过CIColor和CGColor属性打印信息的时候,我们会发现CIColor的色彩空间依然是CMYK,但是CGColor打印所得到的信息说明它已经被转换成RGB空间了。

 

五、UIColor延伸,如何判断两个颜色是否相等

  前面提到一点,不管UIColor使用CIColor,CGColor还是其他方式初始化的,其CGColor属性都是可用的。CoreGraphics中提供一个方法可以判断两个CGColor是否相等,因此我们可以通过判断两个UIColor是否相等,下面是看一个简单的例子:

Copy code
// 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!");
}
Copy code

  The first part of the example is to determine whether the two white UIColors are equal. Although both are white, the color space is different. We can find by running that "The two CGColor is not equal!" is printed. The second part of the example simply creates two UIColors in RGB space. Running the program can see that the two colors are the same.

Guess you like

Origin blog.csdn.net/wu347771769/article/details/52161692