Two days ago, a friend asked me how to determine whether the values of the two colors are equal. I want to determine whether the rgba values of the two colors are equal soon, so I found the uicolor class in the help document and found the function easily:
-(Bool) getred :( cgfloat *) Red Green :( cgfloat *) Green Blue :( cgfloat *) Blue Alpha :( cgfloat *) Alpha;
In this way, we can determine whether the colors of two uicolor objects are equal,CodeAs follows:
1:Enum{
2:Enequal,
3:Ennoteaual,
4:Encannotconvert,
5:};
6:Typedef nsinteger kcompareresult;
7:
8:+ (Kcompareresult) isthesamecolor :( uicolor *) color
9:Redvalue :( cgfloat) rvalue
10:Greenvalue :( cgfloat) gvalue
11:Bluevalue :( cgfloat) bvalue
12:Alphavalue :( cgfloat) avalue
13:{
14:
15:If([Color respondstoselector: @ selector (getred: Green: Blue: Alpha :)]) {
16:
17:Cgfloat redvalue, greenvalue, bluevalue, alphavalue;
18:If([Color getred: & redvalue Green: & greenvalue Blue: & bluevalue ALPHA: & alphavalue]) {
19:If(Redvalue = rvalue & greenvalue = gvalue
20:& Bluevalue = bvalue & alphavalue = avalue ){
21:ReturnEnequal;
22:}
23:Else{
24:ReturnEnnoteaual;
25:}
26:}
27:Else{// Can not convert
28:ReturnEncannotconvert;
29:}
30:}
31:}
However, this function is only supported by ios5.0 and later versions. It cannot be used before version 5.0. Later, I found that the uicolor class has another attribute:
@ Property (nonatomic,Readonly) Cicolor * cicolor
This attribute can get the quartz color reference of the current color, and then you can use the code segment (2) to get the color value.
1:Cgcolorref colorref = color. cgcolor;
2:Nsinteger numbercomponents = cgcolorgetnumberofcomponents (colorref );
3:ConstCgfloat * components = cgcolorgetcomponents (colorref );
4:
5:For(IntI = 0; I <numbercomponents; I ++ ){
6:Nslog (@ "Components % d is % F", I, components [I]);
7:}
Through the test, we can find that if cgcolorref has four components, which represent red, green, blue, and Alpha at one time, the uicolor value can be obtained before ios5.0, after obtaining the color information, you can easily determine whether the two colors are the same.
After the test is completed, it is found that when [uicolor whitecolor] And rgba values are, respectively, the result is encannotconvert, it proves that the uicolor object obtained from whitecolor cannot be converted to rgba. Print the components of cgcolor using the code segment above and find that [uicolor whitecolor] has only two components, which proves that the color is not rgba space.
It seems that I had to find another way. Later I thought that the purpose was to judge whether two uicolors were equal. So I checked them based on two uicolor. cgcolor and found another important function.
BoolCgcolor1_tocolor (cgcolorref color1, cgcolorref color2 );
The Improved Color comparison function is as follows:
1:+ (Bool) isthesamecolor2 :( uicolor *) color1 anothercolor :( uicolor *) color2
2:{
3:If(Cgcolor1_tocolor (color1.cgcolor, color2.cgcolor )){
4:ReturnYes;
5:}
6:Else{
7:ReturnNo;
8:}
9:}
You can determine whether the two uicolors are equal based on whether the cgcolor obtained by uicolor is equal.
The first post I posted. Please click it!