热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

用户界面颜色getHue:饱和:亮度:α:没有回报-UIColorgetHue:saturation:brightness:alpha:returnsNO

Iamusingthefollowingfunctiontochangethesaturation,brightnessandalphaofaUIColor:我正在

I am using the following function to "change" the saturation, brightness and alpha of a UIColor:

我正在使用以下函数来“改变”UIColor的饱和度、亮度和alpha:

//UIColor *color = [self color:[UIColor redColor] saturation:0.5 brightness:0.5 alpha:0.5];

- (UIColor *)color:(UIColor *)color saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha {
    CGFloat h, s, b, a;
    [color getHue:&h saturation:&s brightness:&b alpha:&a];
    return [UIColor colorWithHue:h saturation:(s * saturation) brightness:(b * brightness) alpha:(a * alpha)];
}

Prior to iOS 11 (GM) this was working perfectly fine. However, now [UIColor getHue:saturation:brightness:alpha:] returns NO and the hsba values aren't getting changed.

在ios11 (GM)之前,运行得非常好。然而,现在[UIColor getHue::bright:alpha:]返回NO, hsba值不会改变。

Comment in UIColor.h says:

在用户界面颜色发表评论。h说:

If the receiver is of a compatible color space, any non-NULL parameters are populated and 'YES' is returned. Otherwise, the parameters are left unchanged and 'NO' is returned.

如果接收器是兼容的颜色空间,则会填充任何非空参数,并返回“YES”。否则,参数将保持不变并返回'NO'。

What does "compatible color space" mean here? Do I have to convert color spaces? How do I accomplish that? All the colors in my .xcassets are in sRGB.

“兼容颜色空间”在这里是什么意思?我需要转换颜色空间吗?我怎么做到的?我的.xcassets中的所有颜色都在sRGB中。


EDIT: Kind of a fix is to use the following way to get the HSBA values:

编辑:一种修复方法是使用以下方法获取HSBA值:

CGFloat rTemp, gTemp, bTemp, aTemp;
[color getRed:&rTemp green:&gTemp blue:&bTemp alpha:&aTemp];
CGFloat h, s, b, a;
[[UIColor colorWithRed:rTemp green:gTemp blue:bTemp alpha:aTemp] getHue:&h saturation:&s brightness:&b alpha:&a];

1 个解决方案

#1


5  

It appears that the UIColor getHue:saturation:brightness: method doesn't work if the color's color space is sRGB but it does work if the color's color space is Extended sRGB.

如果颜色的颜色空间是sRGB,那么UIColor getHue::bright: method不起作用,但是如果颜色的颜色空间扩展了sRGB,它就起作用。

So the solution is to update the selected Color Space for each of your colors in your color set asset.

因此,解决方案是更新您的颜色集资产中的每个颜色的选定颜色空间。

This can be demonstrated in a Swift Playground as follows. This creates a color using the sRGB color space.

这可以在一个快速的操场上进行演示,如下所示。这将使用sRGB颜色空间创建一个颜色。

if let cs = CGColorSpace(name: CGColorSpace.sRGB) {
    if let cc = CGColor(colorSpace: cs, components: [0.5, 0.7, 0.3, 1.0]) {
        let color = UIColor(cgColor: cc)
        print(color)
        var h: CGFloat = 0
        var s: CGFloat = 0
        var b: CGFloat = 0
        if color.getHue(&h, saturation: &s, brightness: &b, alpha: nil) {
            print(h, s, b)
        } else {
            print("Failed with color space \(cs)")
        }
    }
}

This gives the output:

这使输出:

kCGColorSpaceModelRGB 0.5 0.7 0.3 1
Failed with color space (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1)

kCGColorSpaceModelRGB 0.5 0.7 0.3 1颜色空间失败(kCGColorSpaceICCBased;kCGColorSpaceModelRGB;sRGB iec61966 - 2.1)

Updating the above code to use the CGColorSpace.extendedSRGB color space gives the following results:

更新上面的代码以使用CGColorSpace。extendedSRGB颜色空间给出如下结果:

UIExtendedSRGBColorSpace 0.5 0.7 0.3 1
0.25 0.571428571428571 0.7

0。7 0。1 0。25


推荐阅读
author-avatar
机智的树獭
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有