A technical post for today. I just learned something new, and those of you using Core Graphics might benefit from my experience.
The documentation for CGColorGetComponents() says that it “Returns the values of the color components (including alpha) associated with a Quartz color.”
More specifically, it’ll give you “An array of intensity values for the color components (including alpha) associated with the specified color. The size of the array is one more than the number of components of the color space for the color.”
Previously, I was assuming that the color space was RGB. That’s not always the case! If you use the convenience methods [UIColor blackColor] or [UIColor whiteColor], you’ll get values in the “White” color space.
If you try to interpret these values as RGB (as I did), your black will look green, and your white will look yellow.
While you could manually do some conversions based on the color space, I’m not confident enough in my knowledge of color spaces to do that yet. (What other color spaces might exist other than the two I know about?) So I decided on a simple workaround:
[UIColor colorWithRed:0 green:0 blue:0 alpha:1] for black;
[UIColor colorWithRed:1 green:1 blue:1 alpha:1] for white;
… and all behaves as expected.
Adversite here
Comments
There are no comments yet. Be the first to comment!
Leave A Comment