Differences and relationships among UIColor, CGColor, and CIColor

Source: Internet
Author: User

Differences and relationships among UIColor, CGColor, and CIColor

Recently I looked at CoreGraphics and saw CGColor. So I thought about taking a look at UIColor and CIColor to find out the differences and relationships between them. Let's take a look at the three concepts:

I. UIColor

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

In addition, UIColor has two important attributes: CGColor and CIColor (added after 5.0 ). The two attributes associate the UIColor, CGColor, and CIColor objects. The conversion between the three objects will be described in detail later.

 

Ii. CGColor

CGColor is mainly used in the CoreGaphics framework. CGColor is actually a struct, and the reference type CGColorRef is usually used when CGColor is used. CGColor consists of two parts: CGColorSapce and Color Components. The Color is the same. If the Color space is different, the parsed results may be different. This is like when we are processing image data, we can imagine the result of processing RGBA format as BGRA format. In Quartz 2D, CGColor is often used to set the fill color of context and set the transparency.

1. How to Create a CGColor, the most common function is CGColorCreate, which has two parameters:

1) colorspace: Specify the color space corresponding to CGColor, and Quartz will retain the object. Therefore, after calling the object, you can safely release the object.

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

This function returns a newly created CGColorRef. When we no longer use this object, use the CGColorRelease function to release this object.

2. Obtain CGColor data

When we create two important parameters, we can get the corresponding ColorSpace and Components after obtaining CGColorRef.

1) Obtain ColorSpace

Through the CGColorGetColorSpace function, we can obtain the ColorSpace corresponding to the current CGColorRef. This function only accepts one parameter, that is, you need to obtain the CGColorRef of the ColorSpace. The following is a simple example:

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

2) obtain Color Components

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

size_t CGColorGetNumberOfComponents (   CGColorRef color);const CGFloat * CGColorGetComponents (   CGColorRef color);

The first function is to obtain the number of color components contained in CGColorRef, and the second function is to obtain the array of the actual color components. The following is 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]);}

 

Iii. 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. In CIColor, the color value ranges from 0.0 to 1.0. 0.0 indicates that the color weight is the minimum value, and 1.0 indicates that the color weight is the maximum value. The alpha value ranges from 0.0 to 1.0. 0.0 indicates full transparency, and 1.0 indicates full opacity. At the same time, the color components of CIColor are usually not multiplied by the alpha value.

We can use the initWithCGColor: function to create a CIColor using CGColor. The passed CGColorRef object can make any color space, but the Core Image framework converts all the color space to the core image work color space before passing in the filter kernel. The core image work color space is composed of three color components plus an alpha component (in fact, kCGColorSpaceDeviceRGB). We will verify this in the following example.

 

Iv. Differences and relationships between UIColor, CGColor, and CIColor

1. Two properties of UIColor: CGColor and CIColor

The CGColor of UIColor is always valid. Whether it is created by using CGColor, CIColor, or other methods, the CGColor attribute is always valid. However, the CIColor attribute is not always valid, this parameter is valid only when UIColor is created through CIColor. Otherwise, an exception will be thrown when you access this attribute. Here is a small example:

// test init uicolor with CGColorUIColor *color = [UIColor colorWithCGColor:[UIColor whiteColor].CGColor];    // CGColor property is always validNSLog(@"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

2. Use CGColor for UIColor Initialization

When UIColor is initialized using CGColor, all information contained in CGColorRef will be retained, including Color space, in addition, the following small example shows that when using CGColor to initialize UIColor, UIColor actually directly retains this CGColorRef object. Example:

// test kCGColorSpaceDeviceCMYKCGColorSpaceRef cmykSpace = CGColorSpaceCreateDeviceCMYK();CGFloat cmykValue[] = {1, 1, 0, 0, 1};      // blueCGColorRef colorCMYK = CGColorCreate(cmykSpace, cmykValue);CGColorSpaceRelease(cmykSpace);NSLog(@"colorCMYK: %@", colorCMYK);    // color with CGColor, uicolor will just retain itUIColor *color = [UIColor colorWithCGColor:colorCMYK];NSLog(@"CGColor from UIColor: %@", color.CGColor);

3. Use CIColor to initialize UIColor.

Next we will discuss how to access the CGColor attribute of UIColor when using CIColor to initialize a UIColor, we will find that the color Space of CGColor is different from the color space of CIColor. In this process, CIColor will make a conversion for us. Next let's take a look at the use of kCGColorSpaceDeviceGray, kCGColorSpaceDeviceRGB, kCGColorSpaceDeviceCMYK three color spaces to initialize a CIColor, then use the CIColor to initialize a UIColor, and then access its CIColor, CGColor attribute, view the color space and print the color information.

1) Use kCGColorSpaceDeviceGray to initialize CIColor

First, check the Code:

 // test kCGColorSpaceDeviceGrayNSLog(@"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 ---> kCGColorSpaceDeviceRGBNSLog(@"cicolor from UIColor: %@", color.CIColor);NSLog(@"cicolor's colorspace: %@", color.CIColor.colorSpace);NSLog(@"color's CGColor: %@", color.CGColor);

By running the program, we can see that if you use the CGColor of the color space of a kCGColorSpaceDeviceGray to initialize CIColor, we can see that the color space of CIColor is always kCGColorSpaceDeviceGray, by accessing the CIColor attribute of UIColor, we can see that its color space is still kCGColorSpaceDeviceGray, but when accessing the CGColor attribute of UIColor, by printing, we can see that the color space has been converted into the kCGColorSpaceDeviceRGB space, and the color value has been correctly converted from the original color space to the new color space.

2) use kCGColorSpaceDeviceRGB to initialize CIColor

Let's look at the Code:

 //test kCGColorSpaceDeviceRGBNSLog(@"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);

Throughout the process, the CIColor and the values accessed through the CGColor and CIColor attributes of UIColor are printed out, and we can find that they are all kCGColorSpaceDeviceRGB space.

4. Use kCGColorSpaceDeviceCMYK to initialize CIColor

Let's continue with the code:

// test kCGColorSpaceDeviceCMYKCGColorSpaceRef cmykSpace = CGColorSpaceCreateDeviceCMYK();NSLog(@"Components number: %zu", CGColorSpaceGetNumberOfComponents(cmykSpace));CGFloat cmykValue[] = {1, 1, 0, 0, 1};      // blueCGColorRef 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 ColorspaceNSLog(@"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 kCGColorSpaceDeviceRGBNSLog(@"cgcolor from UIColor: %@", color.CGColor);

During the entire process, we can also find that when we use a CGColor in the cmyk color space to initialize CIColor, the color space of CIColor is still CMYK, however, the color value has been converted to the RGB color value. When we use this CIColor to create a UIColor, and then print the information through the CIColor and CGColor attributes, we will find that the color space of CIColor is still CMYK, however, the information obtained by CGColor printing indicates that it has been converted to RGB space.

 

5. How to determine whether two colors are equal when UIColor is extended

As mentioned above, the CGColor attribute is available no matter whether UIColor is initialized using CIColor, CGColor, or other methods. CoreGraphics provides a method to determine whether two CGColor values are equal. Therefore, we can determine whether two UIColor values are equal. Here is a simple example:

// judge two CGColor is equalif (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!");}

In this example, The first part is to determine whether two white uicolors are equal. Although they are both white, The color space is different. By running The command, we can find that "The two CGColor is not equal! ". In the second part of the example, the UIColor of the two RGB spaces is simply created. The running program shows that the two colors are the same.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.