iOS Development--layer OC Chapter &uicolor in-depth study (cgcolor,cicolor)

Source: Internet
Author: User
<span id="Label3"></p><p style="text-align: center;"><p style="text-align: center;"><span style="font-size: 18pt;">Uicolor In-depth study (cgcolor,cicolor)<br></span></p></p><p style="text-align: left;"><p style="text-align: left;"><span style="font-size: x-large;"><span style="line-height: 36px;"><span style="font-size: 16px;">Because people like to study the layer and animation technology, is going to see other people write good things, encountered several problems,</span></span></span></p></p><p style="text-align: left;"><p style="text-align: left;"><span style="font-size: x-large;"><span style="line-height: 36px;"><span style="color: #ff6600; font-size: 14pt;">First: the use of Uiclor method</span></span></span></p></p><p style="text-align: left;"><p style="text-align: left;"><span style="font-size: 18px;"><span style="line-height: 36px;">is about the use of uicolor, remember that before the development of all we used are uicolor directly after the use of the method to get the color alive using</span></span></p></p> <ul> <ul> <li style="text-align: left;">+ (uicolor *) colorwith<span style="color: #ff6600;">red</span>:(cgfloat) red <span style="color: #ff6600;">green</span>:(cgfloat) green <span style="color: #ff6600;">blue</span>:(cgfloat) Blue alpha: ( Cgfloat) alpha; But suddenly saw someone using it.</li> <li>+ (uicolor *) colorwith<span style="color: #ff6600;">hue</span>:(cgfloat) hue <span style="color: #ff6600;">saturation</span>:(cgfloat) saturation <span style="color: #ff6600;">Brightness</span>:( Cgfloat) Brightness alpha: (cgfloat) alpha;</li> <li>+ (uicolor *) colorwith<span style="color: #ff6600;">cgcolor</span>:(cgcolorref) cgcolor;</li> <li>+ (uicolor *) colorwith<span style="color: #ff6600;">cicolor</span>:(cicolor *) cicolor ns_available_ios (5_0);</li> </ul> </ul><p><p>Although you usually see when you knock, occasionally will pay attention to, but never used three, also do not know the meaning of the inside (cgcolor used two times, but did not study). 、</p></p><p><p></p></p><p><p>Before this, we will develop a common sense of color: have you found that the front two is a bit familiar, if you can get RGBA/HSBA</p></p><p><p></p></p><p><p>About color categories:<span style="color: #ff6600;">Rgb/hsb/cmyk/lab</span></p></p> <ul> <ul> <li>Lab: The mode consists of three channels, but not r, G, b channels. One of its channels is brightness, that is L. The other two are color channels, denoted by A and B. The a-channel includes color from dark green (bottom luminance Value) to gray (medium luminance Value) to bright pink (high luminance value), b-channel from bright blue (bottom luminance Value) to gray (medium luminance Value) to yellow (high luminance value). therefore, this color mixing will produce bright colors.</li> <li><span style="line-height: 1.5;">HSB: in the pattern, H is the hue, s is the saturation, B is the brightness, and the human eye is the Theory.</span></li> <li><span style="line-height: 1.5;">CMYK: represents four colors used in printing, C for cyan, m for magenta, y for yellow, and K for Black. Because in actual references, cyan, magenta, and yellow are hard to stack to form real black, up to Brown. So the K--black was Introduced. The effect of black is to strengthen the dark tone and deepen the dark Color.</span></li> <li>RGB: is the color mode of the Shade. R is red, G is green, B is blue, and three colors are superimposed to form other Colors. Because three colors have 256 levels of brightness level, Three colors are superimposed to form 16.7 million colors. That is true color, through them enough in the present beautiful WORLD.</li> </ul> </ul><p><p></p></p><p style="text-align: left;"><p style="text-align: left;">Here is to understand, usually we develop the most used is rgb, of course, if you are really using the hsb, you can also be the same as RGB to pass the corresponding Value.</p></p><p><p>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:</p></p><p><p><span style="color: #ff6600; font-size: 14pt;">Second: about the use of uicolor/cicolor/cgcolor differences</span></p></p><p><p><span style="color: #0000ff; font-size: 18px;">first, Uicolor</span></p></p><p><p>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).</p></p><p><p>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.</p></p><p><p></p></p><p><p><span style="font-size: 18px; color: #0000ff;">second, Cgcolor</span></p></p><p><p>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.</p></p><p class="p1"><p class="p1">1, How to create a cgcolor, the most commonly used function is cgcolorcreate, the function has two parameters:</p></p><p class="p1"><p class="p1">1) colorspace, Specify cgcolor corresponding color space, quartz will retain the object, so you can safely release the object after the call is Finished.</p></p><p class="p1"><p class="p1">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.</p></p><p class="p1"><p class="p1">The function should return a newly created cgcolorref and use the Cgcolorrelease function to dispose of the object when we no longer use it.</p></p><p class="p1"><p class="p1">2. Get Cgcolor data</p></p><p class="p1"><p class="p1">When we create, we pass in two important parameters, and when we get to cgcolorref, we can certainly get the corresponding colorspace and Components.</p></p><p class="p1"><p class="p1">1) Get ColorSpace</p></p><p class="p1"><p class="p1">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:</p></p><pre><pre>Cgcolorref Cgcolor = [uicolor redcolor]. cgcolor; Cgcolorspaceref colorspace = Cgcolorgetcolorspace (cgcolor); NSLog (@ "color space:%@", colorspace);</pre></pre><p><p>2) Get color components</p></p><p><p>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:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>size_t cgcolorgetnumberofcomponents ( cgcolorref color); Const cgfloat * cgcolorgetcomponents ( cgcolorref color);</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>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:</p></p><pre><pre>Nsuinteger num = cgcolorgetnumberofcomponents (cgcolor); const cgfloat *colorcomponents = cgcolorgetcomponents (cgColor) ; for (int i = 0; i < num; ++i) { NSLog (@ "color components%d:%f", i, colorcomponents[i]);}</pre></pre><p><p> </p></p><p><p><span style="color: #0000ff; font-size: 14pt;">third, Cicolor</span></p></p><p><p>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.</p></p><p><p>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.</p></p><p><p></p></p><p><p><span style="color: #0000ff; font-size: 14pt;">Iv. differences and linkages of uicolor,cgcolor,cicolor</span></p></p><p><p>1, Uicolor Two Properties Cgcolor,cicolor</p></p><p><p>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:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>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</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>2. Uicolor using Cgcolor initialization</p></p><p><p>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:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>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);</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>3. Uicolor using Cicolor initialization</p></p><p><p>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.</p></p><p><p>1) Initialize Cicolor with Kcgcolorspacedevicegray</p></p><p><p>First look at the Code:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>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);</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>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.</p></p><p><p>2) Initialize Cicolor with Kcgcolorspacedevicergb</p></p><p><p>The same we look at the Code:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>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);</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>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.</p></p><p><p>4. Initialize Cicolor with Kcgcolorspacedevicecmyk</p></p><p><p>Keep looking at the following code:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>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);</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>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.</p></p><p><p></p></p><p><p>V. Uicolor extension, How to determine whether two colors are equal</p></p><p><p>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:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>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!");}</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>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.</p></p><p><p>iOS Development--layer OC Chapter &uicolor in-depth study (cgcolor,cicolor)</p></p></span>
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.