How to get the color components of CGColor

I have a UILabel with black color;

I am writing the following code to get the black components.

UIColor *aColor = [aLabel.textColor retain]; const CGFloat* components = CGColorGetComponents(aColor.CGColor); CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB(); 

but always it gives green components instead of black components;

Does anyone have any ideas about this?

Am I transferring a different color space?

:-( Please help me.

+6
iphone quartz-graphics cgcolor
Nov 11 2018-10-11
source share
5 answers

Most likely the wrong color space. I assume that the alpha component of the grayscale color space gets where you think the green component should be.
I use this function to create a string from UIColor, I only see RGB and Grayscale color spaces, so I just interpret each color with less than 4 (R + G + B + A) grayscale components.

 if (CGColorGetNumberOfComponents(color.CGColor) < 4) { const CGFloat *components = CGColorGetComponents(color.CGColor); color = [UIColor colorWithRed:components[0] green:components[0] blue:components[0] alpha:components[1]]; } if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) { NSLog(@"no rgb colorspace"); // do seomthing } const CGFloat *components = CGColorGetComponents(color.CGColor); NSString *colorAsString = [NSString stringWithFormat:@"%f,%f,%f,%f", components[0], components[1], components[2], components[3]]; 

Of course, this method is not saved for all cases, you have to accept it according to your requirements.

+13
Nov 11 2018-10-11
source share

The reason you see what looks like r=0 , g=1 , b=0 , a=0 is because you incorrectly interpret the values ​​in the returned array as RGB color models. UIColor uses a monochrome color space for shades of gray , such as black .
What you see is an array of 2 components from a monochrome color model . The first is a gray level ( 0 for black), and the second alpha ( 1 for opaque). The last two values ​​you are looking at are at the end of the array of elements 2 and in this case will be 0 .

You will notice that the color is black, and you are trying to execute CGColorGetNumberOfComponents(color.CGColor) , it returns 2 . And if you try CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) , it returns 0 , which corresponds to kCGColorSpaceModelMonochrome (see Listing CGColorSpaceModel in CGColorSpace.h )

see CGColorSpace Reference

+5
Apr 2 '11 at 23:45
source share

I think this is a very good way to get the rgb view of any UIColor * that already has a convenient method for saving its components.

 -(CGColorRef)CGColorRefFromUIColor:(UIColor*)newColor { CGFloat components[4] = {0.0,0.0,0.0,0.0}; [newColor getRed:&components[0] green:&components[1] blue:&components[2] alpha:&components[3]]; CGColorRef newRGB = CGColorCreate(CGColorSpaceCreateDeviceRGB(), components); return newRGB; } 
+2
Nov 28 '11 at 17:00
source share

One way to get RGB is to paint the color in a graphics context and read the color back. See my answer to this question, for example, the code:

Get RGB value from UIColor presets

+1
Jan 15 '11 at 3:25
source share

working exam with UIColor: CALayer * btnLayer = [myLittleButton layer];

[layer setBackgroundColor:<#(CGColorRef)#>] becomes: [btnLayer setBorderColor:[[UIColor grayColor] CGColor]];

just make sure the original button colors do not cover the layer

0
Mar 15 '13 at 14:01
source share



All Articles