The difference and connection of the three Ios-uicolor,cgcolor,cicolor __ios

Source: Internet
Author: User

The difference and connection of the three Uicolor,cgcolor,cicolor

Recently looked at coregraphics things, see About cgcolor things, so think of the way to see Uicolor,cicolor, find out the difference between them and contact. Let's take a look at their three concepts separately:

First, Uicolor

Uicolor is an important class for storing color information in Uikit, and a Uicolor object 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, and the color spaces of these colors are different (white and black are Kcgcolorspacedevicegray, The red color space is Kcgcolorspacedevicergb).

In addition, Uicolor has two important properties: one is Cgcolor and the other is Cicolor (added after 5.0). These two properties can be linked to the Uicolor,cgcolor,cicolor three objects, followed by a detailed description of 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 of its reference type Cgcolorref. Cgcolor is composed mainly 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 picture data, if the RGBA format as the Bgra format processing results can be imagined. Cgcolor is commonly used in quartz 2D to set the fill color of the context, setting 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 after the call you can safely release the object.

2 components, a cgfloat array of elements that specifies the number of color components contained in the color space, plus the corresponding alpha value.

The function should return a newly created cgcolorref and use the Cgcolorrelease function to release the object when we no longer use it.

2, get the Cgcolor data

When we create, we pass in two important parameters, and when we get the 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 to the ColorSpace, the function only accept one parameter is the cgcolorref you want to get colorspace. 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 color value of the cgcolorref, we need to use the cgcolorgetnumberofcomponents and cgcolorgetcomponents two functions. Let's take a look at the function prototypes of two functions first:

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 the actual color components, and 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 used primarily with other classes in the core image framework, such as Cifilter,cicontext and Ciimage. Today we are mainly concerned about the color value section, Cicolor in the range of color values between 0.0-1.0, 0.0 for the color component is the minimum value, 1.0 for the color component is the maximum value. Where Alpha values range from 0.0 to 1.0, 0.0 represents full transparency, 1.0 is completely opaque, and cicolor color components are usually not multiplied by alpha values.

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 frame converts all the color space to the core image working color space before the filter kernel is passed in. The core image working color space is composed of three color components plus an alpha component (in fact, Kcgcolorspacedevicergb), which we verify in the following example.

Iv. differences and links between the Uicolor,cgcolor,cicolor

1, uicolor two properties Cgcolor,cicolor

The cgcolor of Uicolor is always valid, whether it is created through Cgcolor,cicolor or other methods, and the Cgcolor attribute is always valid, but the Cicolor attribute is not always valid. It is only valid if Uicolor is created by Cicolor, otherwise accessing the property throws an exception, as follows a small example:

Test init uicolor with Cgcolor
uicolor *color = [Uicolor colorwithcgcolor:[uicolor whitecolor]. Cgcolor];
    
Cgcolor always valid
NSLog (@ "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 use Cgcolor initialization

When Uicolor uses Cgcolor initialization, all CGCOLORREF contains information that will be preserved intact, including color space, and we can see from the following small example that if you use Cgcolor to initialize Uicolor , Uicolor actually kept a copy of this Cgcolorref object directly. Examples are as follows:

Test Kcgcolorspacedevicecmyk
Cgcolorspaceref cmykspace = Cgcolorspacecreatedevicecmyk ();
CGFloat cmykvalue[] = {1, 1, 0, 0, 1};      Blue
Cgcolorref ColorCMYK = cgcolorcreate (Cmykspace, cmykvalue);
Cgcolorspacerelease (cmykspace);
NSLog (@ "ColorCMYK:%@", ColorCMYK);
    
Color with Cgcolor, uicolor'll just retain it
uicolor *color = [Uicolor Colorwithcgcolor:colorcmyk];
NSLog (@ "Cgcolor from Uicolor:%@", color. Cgcolor);

3, Uicolor use Cicolor initialization

Let's talk about when we use Cicolor to initialize a uicolor, and then to access the Cgcolor properties of Uicolor, we find the color space of cgcolor and the color of the set Cicolor The space is not exactly the same, in the process Cicolor will make a conversion for us. Let's look at the time to initialize a cicolor using Kcgcolorspacedevicegray,kcgcolorspacedevicergb,kcgcolorspacedevicecmyk three color spaces, Then use the Cicolor to initialize a uicolor, then access its Cicolor, Cgcolor properties, view the color space, and print the color information.

1 Initialize Cicolor with Kcgcolorspacedevicegray

First look at the code:

//test Kcgcolorspacedevicegray NSLog (@" 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 spaces to the
Filter kernel. Kcgcolorspacedevicegray---> Kcgcolorspacedevicergb NSLog (@ "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 we use the cgcolor of a kcgcolorspacedevicegray color space to initialize the Cicolor, We can see that Cicolor's color space has been kcgcolorspacedevicegray, and by accessing the Uicolor Cicolor properties, we can see that its color space is still kcgcolorspacedevicegray, However, when accessing the Cgcolor properties of Uicolor, it can be found that the color space has been transformed into Kcgcolorspacedevicergb space, and the color value is transferred from the original color space to the new color space.

2 Initialize Cicolor with Kcgcolorspacedevicergb

The same we look at the code:

Test Kcgcolorspacedevicergb
NSLog (@ "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 the Uicolor Cgcolor and Cicolor properties are printed out and we can see that they are all kcgcolorspacedevicergb space.

4, the use of Kcgcolorspacedevicecmyk initialization Cicolor

Let's continue to look at a piece of code:

//test Kcgcolorspacedevicecmyk cgcolorspaceref cmykspace =
Cgcolorspacecreatedevicecmyk ();
NSLog (@ "Components number:%zu", Cgcolorspacegetnumberofcomponents (Cmykspace));      CGFloat cmykvalue[] = {1, 1, 0, 0, 1};
Blue Cgcolorref 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 colorspace NSLog (@ "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'll convert other colorspace to Kcgcolorspacedevicergb NSLog (@ "Cgcol or from Uicolor:%@ ", color. Cgcolor); 
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.