18

When I call CGColorGetComponents with the CGColor returned from a UIColor, it seems to work properly except with white and black.

Here's the code...

CGColorRef myColorRef = [[UIColor whiteColor] CGColor];

const CGFloat * colorComponents = CGColorGetComponents(myColorRef);

NSLog(@"r=%f, g=%f, b=%f, a=%f",
    colorComponents[0],
    colorComponents[1],
    colorComponents[2],
    colorComponents[3]);

This logs

r=1.000000, g=1.000000, b=0.000000, a=0.000000

Note both B and A are zero, not one.

If you substitute other colors like redColor, blueColor, etc., it works... the RGB and A values are set as one would expect. But again, black and white produce odd results. Is there some issue with this function or is there some workaround/task I should be doing?

1 Answer 1

33

[UIColor whiteColor] and [UIColor blackColor] use [UIColor colorWithWhite:alpha:] to create the UIColor. Which means this CGColorRef has only 2 color components, not 4 like colors created with [UIColor colorWithRed:green:blue:alpha:].

Of course you can NSLog those too.

if (CGColorGetNumberOfComponents(myColorRef) == 2) {
    const CGFloat *colorComponents = CGColorGetComponents(myColorRef);
    NSLog(@"r=%f, g=%f, b=%f, a=%f", colorComponents[0], colorComponents[0], colorComponents[0], colorComponents[1]);
}
else if (CGColorGetNumberOfComponents(myColorRef) == 4) {
    const CGFloat * colorComponents = CGColorGetComponents(myColorRef);
    NSLog(@"r=%f, g=%f, b=%f, a=%f", colorComponents[0], colorComponents[1], colorComponents[2], colorComponents[3]);
}
else {
    NSLog(@"What is this?");
}

Be aware that there are different colorSpaces too. So if you need this code for more than logging (e.g. saving RGBA strings to json) you have to check (and probably convert) the colorSpace too.

8
  • Ok, so starting from a UIColor, how does one set the current fill and stroke colors easily, or do we have to do what you just did there every time we need to do this? (As for color spaces, I'm only interested in the RGBA values as set between 0.0 and 1.0 and displayed on the screen. Anything else will be outside of the scope of what we are doing.) Commented Feb 11, 2012 at 8:45
  • 2
    for a CGContextRef? simply use CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); Commented Feb 11, 2012 at 8:47
  • Also, what's color.CGColor in your code? Is that different than myColorRef? Commented Feb 11, 2012 at 8:48
  • oh, sorry. Forgot to change that to your variable names. color is a UIColor. So color.CGColor is the same as your myColorRef Commented Feb 11, 2012 at 8:49
  • Aaah! The whole reason we were getting the components was to call CGContextSetColor. Didn't know about the 'WithColor' variant that basically does that for us. I must've overlooked that. Perfect! Commented Feb 11, 2012 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.