2013-12-08 37 views
0

如果我運行下面的代碼:CGColorComponents返回黑色(0,0,0)爲的UIColor whiteColor

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]; 
} 

的紅,綠,藍返回0每個NSLog的;然而,如果我給它任何其他顏色,它會顯示我希望它顯示的顏色。

這是爲什麼?

回答

3

像白色和黑色的顏色使用單色色空間(黑色/白色,阿爾法)而不是(紅色,綠色,藍色)表示。 的numComponents將返回2,因此您的if塊永遠不會執行。

+0

噢好吧,這是有道理的。謝謝它的作品! – David

相關問題