recently looked at coregraphics things, see About the Cgcolor things, so think about the way to see Uicolor,cicolor, find out the difference between them and contact. Let's take a look at the three concepts below: First, Uicolor Uicolor is an important class for storing color information in Uikit, a Uicolor object that contains values for color and transparency, and its color space has been optimized for iOS. Uicolor contains some class methods for creating some of the most common colors, such as white, black, red, transparent, and so on, where the color space is also different (white and black are Kcgcolorspacedevicegray, The red color space is Kcgcolorspacedevicergb). In addition Uicolor has two important properties: One is Cgcolor, the other is Cicolor (5add after. 0). These two properties can be used to connect the Uicolor,cgcolor,cicolor three objects, which will be described in detail later in the conversion between the three. Second, Cgcolor Cgcolor is mainly used in coregaphics framework, Cgcolor is actually a structure, and we usually use the Cgcolor when using its reference type Cgcolorref. Cgcolor is mainly composed of the cgcolorsapce and color components two parts, the same color composition, if the color space is different, the results may vary. This is like when we are dealing with the image data, if the RGBA format as Bgra format processing results imaginable. In Quartz 2D, cgcolor is commonly used to set the context's fill color, set transparency, and so on. 1, how to create a cgcolor, the most common function is cgcolorcreate, the function has two parameters:1colorspace, specify the color space for the cgcolor, and quartz will retain the object, so you can safely dispose of the object after the call is finished. 2components, an array of cgfloat, the number of elements of the array is the specified color space contains the color component n, plus the corresponding alpha value. The function should return a newly created cgcolorref and use the Cgcolorrelease function to dispose of the object when we no longer use it. 2, get Cgcolor data in our creation when we pass in two important parameters in, when we get to cgcolorref, of course we can get the corresponding colorspace and components. 1get colorspace through the Cgcolorgetcolorspace function we can get to the current cgcolorref corresponding to the ColorSpace, The function accepts only one parameter, which is the cgcolorref you want to get colorspace. Let's look at a simple example: Cgcolorref cgcolor=[Uicolor Redcolor]. Cgcolor; Cgcolorspaceref ColorSpace=Cgcolorgetcolorspace (Cgcolor); NSLog (@"Color space:%@", ColorSpace); 2Get color components to get to the cgcolorref corresponding color values, we need to use the cgcolorgetnumberofcomponents and cgcolorgetcomponents two functions. Let's take a look at the function prototypes of the two functions: Copy code size_t cgcolorgetnumberofcomponents (cgcolorref color);ConstCGFloat *cgcolorgetcomponents (cgcolorref color); The first function to copy the code is to get the number of color components contained in the Cgcolorref, and the second function is to get an array of the actual color components. Here's a small example: Nsuinteger num=cgcolorgetnumberofcomponents (cgcolor);ConstCGFloat *colorcomponents =cgcolorgetcomponents (cgcolor); for(inti = 0; i < num; ++i) {NSLog (@"Color components%d:%f", I, Colorcomponents[i]);} Cicolor Cicolor is mainly used for other classes in the core image framework, such as Cifilter,cicontext and Ciimage. Today we are primarily concerned with the color value section, where the range of color values in Cicolor is 0.Between 0-1.0, 0.0 represents the minimum value of the color component, and 1.0 represents the maximum value of the color component. Where Alpha values range from 0.0 to 1.0, 0.0 for full transparency, 1. 0 is completely opaque, and the color component of the cicolor is usually not multiplied by the alpha value. We can use the Initwithcgcolor: function to create a cicolor through Cgcolor. The incoming Cgcolorref object can make any color space, but the core image framework converts all of the color space to the core image working color space before passing in the filter kernel. The core image working color space uses three color components plus an alpha component (in fact, Kcgcolorspacedevicergb), which we verify in the following example. Iv. differences and linkages of Uicolor,cgcolor,cicolor1, Uicolor two properties Cgcolor,cicolor uicolor Cgcolor is always valid, whether it is created by Cgcolor,cicolor or by other methods, Cgcolor properties are always valid But the Cicolor property is not always valid, only if Uicolor is created through Cicolor, he is valid, otherwise access to the property will throw an exception, the following is a small example: Copy code//Test init uicolor with CgcolorUicolor *color =[Uicolor Colorwithcgcolor:[uicolor Whitecolor]. Cgcolor]; //Cgcolor Property was always validNSLog (@ "Cgcolor from Uicolor%@", color. Cgcolor);//don ' t use the Cicolor property//The throws an exception if the color object is not initialized with a Core Image color.NSLog (@ "Cicolor from Uicolor%@", color. Cicolor);//CrushCopy Code2, Uicolor uses Cgcolor initialization when Uicolor uses Cgcolor initialization, all the information contained in the CGCOLORREF is retained intact, including the color space, And by the following small example, we can also see that if you use Cgcolor to initialize Uicolor, Uicolor actually retains a copy of this Cgcolorref object directly. Examples are: Copy code//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 would just retain itUicolor *color =[Uicolor Colorwithcgcolor:colorcmyk]; NSLog (@"Cgcolor from Uicolor:%@", color. cgcolor); Copy Code3, uicolor using Cicolor initialization Let's talk about it. When you use Cicolor to initialize a uicolor, and then go to Uicolor's Cgcolor property, we find that the Cgcolor color Space and set Cicolor color space is not exactly the same, in this process cicolor will do a conversion for us. Let's take a look at the use of Kcgcolorspacedevicegray,kcgcolorspacedevicergb,kcgcolorspacedevicecmyk three color space to initialize a cicolor, Then use the Cicolor to initialize a uicolor, and then go to the Cicolor, Cgcolor properties, view the color space and print the color information. 1use Kcgcolorspacedevicegray to initialize Cicolor First look at 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 code by running the program, we see that if you use a kcgcolorspacedevicegray color space of Cgcolor to initialize Cicolor, We can see that Cicolor's color space has always been kcgcolorspacedevicegray, by accessing the Uicolor's Cicolor attribute, we can see that its color space is still kcgcolorspacedevicegray, However, when accessing the Cgcolor attribute of Uicolor, printing can find that its color space has turned into kcgcolorspacedevicergb space, and the color value is correctly converted from the original color space to the new color space. 2use Kcgcolorspacedevicergb to initialize Cicolor the same way we look at the 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 Uicolor:%@", color. Cicolor); NSLog (@"Cicolor ' s colorspace:%@", color. Cicolor.colorspace); NSLog (@"Color ' s Cgcolor:%@", color. Cgcolor), copy the code throughout the process Cicolor, and through the Uicolor Cgcolor and Cicolor property access to the values, print out we can find that they are kcgcolorspacedevicergb space. 4, use Kcgcolorspacedevicecmyk to initialize Cicolor below to continue to see a piece of code: copy 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 have 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 would convert other colorspace to KcgcolorspacedevicergbNSLog (@ "Cgcolor from Uicolor:%@", color. Cgcolor); Copying the code throughout the process, we can also find that, when we use a CMYK color space cgcolor to initialize Cicolor, the cicolor color space is still CMYK, but the color value has been converted to RGB color values. When we use the Cicolor to create a uicolor, we then print the information through the Cicolor and Cgcolor properties, we will find that the cicolor color space is still CMYK, However, the information obtained by Cgcolor printing indicates that it has been converted into RGB space. V. Uicolor extension, how to determine whether the two colors are equal before mentioning a point, whether uicolor use Cicolor,cgcolor or otherwise initialized, its Cgcolor properties are available. Coregraphics provides a method to determine whether the two cgcolor are equal, so we can determine whether the two uicolor are equal, here is a simple example: Copy code//Judge Cgcolor is equalif(Cgcolorequaltocolor ([Uicolor Whitecolor]. Cgcolor, [Uicolor colorwithred:1 green:1 blue:1 alpha:1]. Cgcolor)) {NSLog (@"The Cgcolor is equal!");}Else{NSLog (@"The 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 Cgcolor is equal!");}Else{NSLog (@"The Cgcolor is not equal!");} The first part of the copy code example is to determine whether the two white uicolor are equal, although all white, but the color space is different, by running we can find that print out "The two cgcolor is not equal!”。 The second part of the example simply creates a uicolor of two RGB spaces, and the running program can see that the two colors are the same.
Go The difference and connection between the three Uicolor,cgcolor,cicolor