The problem is with the example you give. The color #CCCCCC
is a greyscale color meaning the UIColor
is not created with RGB but with a grey. In other words, it is created with UIColor colorWithWhite:alpha:
. Since your UIColor
is created with a different color model than RGB, using getRed:green:blue:alpha:
returns NO.
What you can do is this:-
UIColor *color = // your color
CGFloat red, green, blue, alpha;
if (![color getRed:&red green:&green blue:&blue alpha:&alpha]) {
CGFloat white;
if ([color getWhite:&white alpha:&alpha]) {
red = green = blue = white;
} else {
NSLog(@"Uh oh, not RGB or greyscale");
}
}
Update:
It seems that the behavior changed a bit as of iOS 8. The RGB values of a color created with UIColor colorWithWhite:alpha:
can be obtained with UIColor getRed:green:blue:alpha:
. In earlier versions of iOS, this didn't work. Hence the need for the code I originally posted.