views:

807

answers:

3

I'm trying to extract the rgb components of a UIColor in order to hand-build the pixels in a CGBitmapContext. The following sample code works fine for most of the UIColor constants but, confusingly, not all. To wit:

CGColorRef color = [[UIColor yellowColor] CGColor];
const float* rgba = CGColorGetComponents(color);

float r = rgba[0];
float g = rgba[1];
float b = rgba[2];
float a = rgba[3];

NSLog( @"r=%f g=%f b=%f a=%f", r, g, b, a);

The results for [UIColor yellowColor] above are
r=1.000000 g=1.000000 b=0.000000 a=1.000000, as expected.

[UIColor redColor] gives r=1.000000 g=0.000000 b=0.000000 a=1.000000, again as expected. Similarly for blueColor and greenColor.

However, the results for [UIColor blackColor] and [UIColor whiteColor] seem completely anomalous, and I don't know what I'm doing wrong (if indeed I am).

To wit, [UIColor blackColor] gives r=0.000000 g=1.000000 b=0.000000 a=0.000000, which is a tranparent green,

and [UIColor whiteColor] gives r=1.000000 g=1.000000 b=0.000000 a=0.000000, which is a transparent yellow.

I'd appreciate it if somebody could either:

(1) explain what I'm doing wrong
(2) replicate my anomalous results and tell me it's not me, or
(3) hit me over the head with a big hammer so it stops hurting so much.

Howard

+1  A: 

I think the thing you're missing is that colors can be defined in different color spaces: you're assuming they're all RGBA.

For instance, the docs state that blackColor "Returns a color object whose grayscale value is 0.0 and whose alpha value is 1.0". So I think only two components are valid for black, not four.

unwind
+6  A: 

You are assuming the color space is always RGBA, which is not the case.

Try this code:

CGColorRef color = [[UIColor blackColor] CGColor];
const CGFloat* rgba = CGColorGetComponents(color);
CGColorSpaceRef space = CGColorGetColorSpace(color);
CGColorSpaceModel model = CGColorSpaceGetModel(space);
NSLog(@"%d", model);

And model will yield kCGColorSpaceModelMonochrome.

pgb
Brilliant! Thank you. I've been confused before by the frequent reference to `CGColorGetNumberOfComponents(color)` before calling `CGColorGetComponents(color)`. Now I see why. By the way, isn't there a way of printing out the *name* of the model above, and not just its value?
hkatz
The debugger will show you the name, I'm not sure you can print it, since it's a const definition of a number.
pgb
A: 

can we get the component for black color from colorspace? how?

Deepika