About UIColor, is this a bug or something else?

I used to think that [UIColor whiteColor] was exactly the same as [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 1], but it turns out that it is 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 uncomfortable .. very unpleasant

+4
source share
1 answer

From the docs for UIColor getRed:green:blue:alpha: ::

If the color is in a compatible color space, the color is converted to RGB, and its components are returned to your application. If the color does not match the compatible color space, the settings are not changed.

[UIColor whiteColor] , and grayscale created using UIColor colorWithWhite:alpha: have a different color space than the RGB color space. Therefore, they are not in a compatible color space. Therefore, using UIColor getRed:green:blue:alpha: with such colors is impractical.

Try initializing 4 CGFloat values ​​and see what you get. If you set them to zero, they should remain equal to zero.

+6
source

Source: https://habr.com/ru/post/1441977/


All Articles