If I run the following code:

CGColorRef cgcolor = [[UIColor whiteColor] CGColor];

int numComponents = CGColorGetNumberOfComponents(cgcolor);

CGFloat red = 0, green = 0, blue = 0;

if (numComponents >= 3)
{
    const CGFloat *components = CGColorGetComponents(cgcolor);
    red = components[0];
    green = components[1];
    blue = components[2];
}

An NSLog of red, green, and blue returns 0 for each; however, if I give it any other color, it displays the color I want it to display.

Why is this?

有帮助吗?

解决方案

Colors like white and black are represented using the monochrome color space (black/white, alpha) as opposed to (red, green, blue). An NSLog of numComponents would return 2, so your if block is never executed.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top