The difference between the Uicolor class of IOS and its related classes and the method of judging equality _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:

 code as follows:

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:







 code as follows:

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:


 code as follows:

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:







 code as follows:

Test init Uicolor with Cgcolor
Uicolor *color = [Uicolor colorwithcgcolor:[uicolor whitecolor]. Cgcolor];

Cgcolor property is always valid
NSLog (@ "Cgcolor from Uicolor%@", color. Cgcolor);


Don ' t use Cicolor
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:





 code 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:





 code as follows:

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:




code as follows:


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:





 code as follows:

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 (@ "Cgcolor from Uicolor:%@", color. Cgcolor);






Throughout the process, we can also find by running, when we use a CMYK color space cgcolor to initialize the Cicolor, 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 shows that it has been converted into RGB space.



Five, Uicolor extension, how to determine whether two colors are equal

As mentioned earlier, the Cgcolor properties are available regardless of whether Uicolor is initialized with Cicolor,cgcolor or otherwise. Coregraphics provides a way to determine whether two cgcolor are equal, so we can determine whether two uicolor are equal, and here's a simple example:





 code as follows:

Judge two Cgcolor is equal
if (Cgcolorequaltocolor ([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 (Cgcolorequaltocolor ([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!");
}





The first part of the example is to determine whether two white uicolor are equal, although all are white, but the color space is different, by running we can find out that "the two Cgcolor is not equal!". The second part of the example simply creates two uicolor of RGB space, and the running program shows that the two colors are the same.






Determine if the color values of two uicolor are equal
Two days ago a friend asked me how to determine whether the values of two colors are equal, I think as long as the RGBA value of two color to determine whether the equivalent soon, so start to find the help document found the Uicolor class, it is easy to find the function:


 code as follows:

-(BOOL) getred: (CGFloat *) Red green: (CGFloat *) Green blue: (CGFloat *) Blue Alpha: (CGFloat *) Alpha;

This allows you to determine whether the color of two Uicolor objects is equal, as follows:
 code as follows:

enum {
Enequal,
Ennoteaual,
Encannotconvert,
};
typedef Nsinteger Kcompareresult;





+ (Kcompareresult) Isthesamecolor: (uicolor*) color
Redvalue: (cgfloat) RValue
Greenvalue: (cgfloat) Gvalue bluevalue: (cgfloat) bvalue
Alphavalue: (cgfloat) Avalue
{



if ([Color respondstoselector: @selector (getRed:green:blue:alpha:)]) {



CGFloat Redvalue, Greenvalue, Bluevalue, Alphavalue;
if ([Color Getred:&redvalue green:&greenvalue blue:&bluevalue Alpha:&alphavalue]) {
if (Redvalue = rValue && greenvalue = gvalue && bluevalue = bvalue && alphavalue = avalue) {
return enequal;
}
else {
return ennoteaual;
}
}
else {//Can not convert
return encannotconvert;
}
}
}





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.