Reputation: 612
I used to think [UIColor whiteColor] was exactly same as [UIColor colorWithRed:1 green:1 blue:1 alpha:1], but it turns out it's not! Can anyone explain this ?
CGFloat red1, green1, blue1, alpha1;
UIColor * color1 = [UIColor whiteColor];
[color1 getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1];
NSLog(@"%f, %f, %f, %f", red1, green1, blue1, alpha1);
UIColor * color2 = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
[color2 getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1];
NSLog(@"%f, %f, %f, %f", red1, green1, blue1, alpha1);
output:
0.000000, 0.000000, 0.000000, -1.998628
1.000000, 1.000000, 1.000000, 1.000000
This is uncool.. very unpleasent
Upvotes: 4
Views: 270
Reputation: 318774
From the docs for UIColor getRed:green:blue:alpha:
:
If the color is in a compatible color space, the color is converted into RGB format and its components are returned to your application. If the color is not in a compatible color space, the parameters are unchanged.
[UIColor whiteColor]
and shades of gray created with UIColor colorWithWhite:alpha:
are from a different color space than the RGB color space. Therefore they are not in a compatible color space. So it is inappropriate to use UIColor getRed:green:blue:alpha:
with such colors.
Try initializing the 4 CGFloat values and see what you get. If you set them all to zero, they should stay zero.
Upvotes: 6