CGColorGetComponents有问题吗? [英] Is there an issue with CGColorGetComponents?

查看:103
本文介绍了CGColorGetComponents有问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从 UIColor CGColor 调用 CGColorGetComponents 时$ c>,它似乎正常工作除了白色和黑色。

When I call CGColorGetComponents with the CGColor returned from a UIColor, it seems to work properly except with white and black.

这是代码......

Here's the code...

CGColorRef myColorRef = [[UIColor whiteColor] CGColor];

const CGFloat * colorComponents = CGColorGetComponents(myColorRef);

NSLog(@"r=%f, g=%f, b=%f, a=%f",
    colorComponents[0],
    colorComponents[1],
    colorComponents[2],
    colorComponents[3]);

此日志

r=1.000000, g=1.000000, b=0.000000, a=0.000000

注意B和A都是零,而不是一个。

Note both B and A are zero, not one.

如果你替换其他颜色,如redColor,blueColor等,它可以工作......设置RGB和A值正如人们所料。但同样,黑色和白色产生奇怪的结果。这个功能有问题,还是我应该做一些变通办法/任务?

If you substitute other colors like redColor, blueColor, etc., it works... the RGB and A values are set as one would expect. But again, black and white produce odd results. Is there some issue with this function or is there some workaround/task I should be doing?

推荐答案

[UIColor whiteColor] [UIColor blackColor] 使用 [UIColor colorWithWhite:alpha:] 创建UIColor。这意味着这个CGColorRef只有2个颜色组件,而不是用 [UIColor colorWithRed:green:blue:alpha:] 创建的4种颜色。

[UIColor whiteColor] and [UIColor blackColor] use [UIColor colorWithWhite:alpha:] to create the UIColor. Which means this CGColorRef has only 2 color components, not 4 like colors created with [UIColor colorWithRed:green:blue:alpha:].

当然你也可以NSLog那些。

Of course you can NSLog those too.

if (CGColorGetNumberOfComponents(myColorRef) == 2) {
    const CGFloat *colorComponents = CGColorGetComponents(myColorRef);
    NSLog(@"r=%f, g=%f, b=%f, a=%f", colorComponents[0], colorComponents[0], colorComponents[0], colorComponents[1]);
}
else if (CGColorGetNumberOfComponents(myColorRef) == 4) {
    const CGFloat * colorComponents = CGColorGetComponents(myColorRef);
    NSLog(@"r=%f, g=%f, b=%f, a=%f", colorComponents[0], colorComponents[1], colorComponents[2], colorComponents[3]);
}
else {
    NSLog(@"What is this?");
}






请注意,有不同的颜色也是空间。因此,如果您需要此代码而不是日志记录(例如将RGBA字符串保存到json),您还必须检查(并可能转换)colorSpace。


Be aware that there are different colorSpaces too. So if you need this code for more than logging (e.g. saving RGBA strings to json) you have to check (and probably convert) the colorSpace too.

这篇关于CGColorGetComponents有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆