Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get color components of a CGColor correctly

I have a UILabel with black color;

i am writing the following code to get black color's components.

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

but always this is giving Green color components instead of black color components;

Is any one have idea about this?

Am i passing different color space?

:-( Please help me.

like image 947
Ganesh Avatar asked Nov 11 '10 14:11

Ganesh


People also ask

How do I get RGB values from UIColor Swift?

let swiftColor = UIColor(red: 1, green: 165/255, blue: 0, alpha: 1) println("RGB Value is:"); println(swiftColor.

What is CG colors?

CGColor is the fundamental data type used internally by Core Graphics to represent colors. CGColor objects, and the functions that operate on them, provide a fast and convenient way of managing and setting colors directly, especially colors that are reused (such as black for text).


3 Answers

Most likely wrong colorspace. I guess the alpha component of the grayscale colorspace gets where you think the green component should be.
I use this function to create a string from UIColor, I only encounter RGB and Grayscale colorspaces, so I just interpret every color with less than 4 (R+G+B+A) components as grayscale.

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 save for all cases, you should adopt it to your requirements.

like image 170
Matthias Bauch Avatar answered Nov 12 '22 06:11

Matthias Bauch


The reason you are seeing what looks like r=0, g=1, b=0, a=0 is because you are misinterpreting the values in the returned array as being in an RGB color model. UIColor uses monochrome colorspace for greyscale colors like black in this case.
What you are seeing is an array of 2 components from a monochrome color model. The first is gray level (0 for black) and the second is alpha (1 for opaque). The last two values your are looking at are off the end of the 2 element array and happen to be 0 in this case.

You'll notice if color is black and you try CGColorGetNumberOfComponents(color.CGColor), it returns 2. And if you try CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)), it returns 0 which corresponds to kCGColorSpaceModelMonochrome (see the enum CGColorSpaceModel in CGColorSpace.h)

see CGColorSpace Reference

like image 33
Logachu Avatar answered Nov 12 '22 05:11

Logachu


I think this is a very nice way to get the rgb representation of any UIColor*, which already has a convenience method for retaining it´s 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;
}
like image 40
kreuzundqwertz Avatar answered Nov 12 '22 05:11

kreuzundqwertz