这是获取颜色的 RGB 分量的代码片段。如果 _countComponents 小于四个,例如两个,我该怎么办?我试图获取灰色组件 [UIColor grayColor]
int _countComponents = CGColorGetNumberOfComponents(colorRef);
if (_countComponents == 4) {
const CGFloat *_components = CGColorGetComponents(colorRef);
CGFloat red = _components[0];
CGFloat green = _components[1];
CGFloat blue = _components[2];
CGFloat alpha = _components[3];
最佳答案
如果你有一个 UIColor 实例并且你想要它的 RGB 值,为什么不使用 -getRed:green:blue:alpha:
方法?
CGFloat r, g, b, a;
BOOL success = [myColor getRed:&r green:&g blue:&b alpha:&a];
if (success) {
// the color was converted to RGB successfully, go ahead and use r,g,b, and a.
}
https://stackoverflow.com/questions/9383784/