Extracting rgb from UIColor [duplicate]

Seen this asked before but my example does not seem to work.

const CGFloat *toCol = CGColorGetComponents([[UIColor greenColor] CGColor]);

The array is empty from looking at it with GDB. Any hints?


The code sample you provided should work.

Try this:

UIColor uicolor = [[UIColor greenColor] retain];
CGColorRef color = [uicolor CGColor];

int numComponents = CGColorGetNumberOfComponents(color);

if (numComponents == 4)
{
  const CGFloat *components = CGColorGetComponents(color);
  CGFloat red = components[0];
  CGFloat green = components[1];
  CGFloat blue = components[2];
  CGFloat alpha = components[3];
}

[uicolor release];

In iOS 5 you could use:

UIColor *color = [UIColor orangeColor];
CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;

if ([color respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
    [color getRed:&red green:&green blue:&blue alpha:&alpha];
} else {
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    red = components[0];
    green = components[1];
    blue = components[2];
    alpha = components[3];
}