) UIColor, CGColor, CIColor, and uicolorcgcolor

Source: Internet
Author: User

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. Next, let's take a look at these three concepts: 1. UIColor is an important class in UIKit for storing color information. A UIColor object contains values of color and transparency, 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. 2. CGColor is mainly used in the CoreGaphics framework. CGColor is actually a struct, And the CGColor we usually use is its reference type CGColorRef. 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. This function has two parameters: 1) colorspace. If you specify the color space corresponding to CGColor, Quartz will retain this object, therefore, after the call, 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 CGColorRef, we input two important parameters. When we obtain CGColorRef, we can obtain the corresponding ColorSpace and Components. 1) obtain the ColorSpace. Through the CGColorGetColorSpace function, we can obtain the ColorSpace corresponding to the current CGColorRef. This function accepts only 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) Get the Color Components and obtain the color value corresponding to CGColorRef, CGColorGetNumberOfComponents and CGColorGetComponents are required. Let's take a look at the prototype of the two functions: copy the code size_t CGColorGetNumberOfComponents (CGColorRef color); const CGFloat * CGColorGetComponents (CGColorRef color ); copy the code. The first function is to obtain the number of color components contained in CGColorRef. 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, colorCompone CTS [I]);} 3. CIColor is mainly used for 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. CGColor, The CGColor attribute of UIColor, and CGColor of CIColor UIColor are always valid, whether it is through CGColor, CIColor, the CGColor attribute is always valid, but the CIColor attribute is not always valid. It is valid only when the UIColor is created through CIColor, otherwise, an exception will be thrown when you access this attribute. The following is a small example: copy the code // 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 copy code 2. UIColor uses CGColor for initialization. When UIColor uses CGColor for initialization, 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: copy the code // test kCGColorSpaceDeviceCMYKCGColorSpaceRef cmykSpace = CGColorSpaceCreateDeviceCMYK (); CGFloat cmykValue [] = {1, 1, 0, 0, 1 }; // blueCGColorRef colorCMYK = CGColorCreate (cmykSpace, cmykValue); cgcolorspacerelspace (cmykSpace); NSLog (@ "colorCMYK: % @", colorCMYK); // color with CGColor, uicolor will just retain itUIColor * color = [UIColor colorWithCGColor: colorCMYK]; NSLog (@ "CGColor from UICol Or: % @ ", color. CGColor); copy code 3. UIColor uses CIColor to initialize. Next we will discuss how to access the CGColor attribute of UIColor when CIColor is used 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, read the code: copy 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); copy the code and run the program. We can see that if CGColor of the color space of a kCGColorSpaceDeviceGray is used 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 the color space is still kCGColorSpaceDeviceGray. However, 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 see the same code: copy 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 U IColor: % @ ", color. CIColor); NSLog (@ "cicolor's colorspace: % @", color. CIColor. colorSpace); NSLog (@ "color's CGColor: % @", color. CGColor); copy the CIColor throughout the Code process, as well as the values accessed through the CGColor and CIColor attributes of UIColor. print them out and we can find that they are all kCGColorSpaceDeviceRGB space. 4. Use kCGColorSpaceDeviceCMYK to initialize CIColor. Next, read the following code: copy the code // test using cmykSpace = CGColorSpaceCreateDeviceCMYK (); NSLog (@ "Components number: % zu ", values (cmykSpace); CGFloat cmykValue [] = {1, 1, 0, 0, 1}; // blueCGColorRef colorCMYK = CGColorCreate (cmykSpace, cmykValue); cgcolorspacerelspace (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 whole process of copying the code, we can also find that when we use CGColor of a 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. V. UIColor Extension: how to determine whether two colors are equal? 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: copy the code // judge two CGColor is invalid if (cgcolor=tocolor ([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 (cgcolor=tocolor ([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 the copy code example, the first part is to judge whether two white uicolors are equal. Although they are both white, the color space is different. We can see through running the command that, print The "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.

 

Related Article

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.