views:

4414

answers:

2

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?

+6  A: 

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];
Pablo Santa Cruz
OK will try this in a moment, thanks
Why is "retain" needed here? Also, I can only seem to get 2 (not 4) components.
Susanna
FYI, Susanna, grayscale colors show up as only 2 components.
davidcann
A: 

This article helped me solve this problem or at least build the framework to solve it.

http://developer.apple.com/iphone/library/qa/qa2007/qa1509.html

OhioDude