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 these three concepts separately:
First, Uicolor
Uicolor is an important class for storing color information in Uikit, a Uicolor object that contains the values of 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 (added after 5.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 the Coregaphics framework, Cgcolor is actually a struct, 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 commonly used function is cgcolorcreate, the function has two parameters:
1) colorspace, specify cgcolor corresponding color space, quartz will retain the object, so you can safely release the object after the call is finished.
2) components, 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
When we create, we pass in two important parameters, and when we get to cgcolorref, we can certainly get the corresponding colorspace and components.
1) Get ColorSpace
Through the Cgcolorgetcolorspace function we can get to the current cgcolorref corresponding colorspace, the function only accept one parameter is you want to get ColorSpace cgcolorref. Let's take a 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 corresponding color values for cgcolorref, we need to use the cgcolorgetnumberofcomponents and cgcolorgetcomponents two functions. Let's take a look at the function prototypes of the two functions:
size_t cgcolorgetnumberofcomponents ( cgcolorref color); Const cgfloat * cgcolorgetcomponents ( cgcolorref color);
The first function is to get the number of color components contained in the Cgcolorref, and the second function is to get an array of actual color components, here's 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]);}
Third, Cicolor
Cicolor is primarily used with other classes in the core image framework, such as Cifilter,cicontext and Ciimage. Today we are mainly concerned about the color value section, the range of color values in Cicolor is between 0.0-1.0, 0.0 for the color component is the minimum value, and 1.0 for the change color component to the maximum value. Where the alpha value ranges from 0.0 to 1.0, 0.0 is full transparent, 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,cicolor
1, uicolor two properties Cgcolor,cicolor
The cgcolor of Uicolor is always valid, whether it is created by Cgcolor,cicolor or by other methods, the Cgcolor property is always valid, but the Cicolor attribute is not always valid. Only if Uicolor is created through Cicolor, he is valid, otherwise access to the property will throw an exception, as a small example:
Test init uicolor with cgcoloruicolor *color = [Uicolor colorwithcgcolor:[uicolor whitecolor]. Cgcolor]; Cgcolor property was always Validnslog (@ "Cgcolor from Uicolor%@", color. Cgcolor);//don ' t use Cicolor property//This property throws an exception if the color object is not initialized with a Core Image color. NSLog (@ "Cicolor from Uicolor%@", color. Cicolor); Crush
2. Uicolor using Cgcolor initialization
When Uicolor initializes with Cgcolor, all Cgcolorref contains information that is preserved intact, including the color space, and through the following small example we can also see if using Cgcolor to initialize Uicolor , Uicolor actually retained a copy of this Cgcolorref object directly. Examples are as follows:
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);
3. Uicolor using Cicolor initialization
Let's talk about when we use Cicolor to initialize a uicolor, and then go to the Cgcolor property of Uicolor, we'll find the color space of cgcolor and the color of Cicolor set 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.
1) Initialize Cicolor with Kcgcolorspacedevicegray
First look at 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 F Ilter 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 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.
2) Initialize Cicolor with Kcgcolorspacedevicergb
The same we 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);
The entire process is cicolor, and the values accessed through Uicolor's Cgcolor and Cicolor properties are printed out and we can see that they are all kcgcolorspacedevicergb spaces.
4. Initialize Cicolor with Kcgcolorspacedevicecmyk
Keep looking at the following 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 (@ "Cgcolo R from Uicolor:%@ ", color. Cgcolor);
Throughout the process, we can also find that when we use a CMYK color space cgcolor to initialize the 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 two colors are equal
As mentioned earlier, Cgcolor properties are available regardless of whether Uicolor is initialized with Cicolor,cgcolor or otherwise. 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:
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 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.
The difference and connection between the three Uicolor,cgcolor,cicolor