objective-c - 在 objective-c 中获取颜色分量

这是获取颜色的 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/

相关文章:

css - 使用 CSS 选择类下面的类

php - 返回图像以在浏览器中显示

amazon-s3 - 如何使用 Google Cloud Storage 隐藏真实 URL?

mercurial - 替换 hg 分支的内容

php - FOS 用户包安装错误

asp.net-mvc - 我怎样才能通过 LCID 获得文化

php - SimplePie 可以从提要中抓取图像吗

php - 如何将 child 添加到特定节点

asp.net-mvc-3 - MVC 3 POST 数据和 Id 字段

sql - 使用 SQL Server Management Studio 自动导出到 CSV