CGColorGetComponents()没有返回黑色和白色的正确值 [英] CGColorGetComponents() not returning the correct values for black and for white

查看:118
本文介绍了CGColorGetComponents()没有返回黑色和白色的正确值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人遇到过这个问题?它看起来像Apple的代码中的一个错误,如果人们同意这个代码应该做我认为应该做的事情,我会提交一个雷达。我不熟悉 CoreGraphics 色彩管理,因此我在问我错误之前向Apple提出错误报告。

Has anyone experienced this problem? It looks like a bug in Apple's code and I will submit a radar if people agree that this code should do what I think it should do. I'm not all that familiar with CoreGraphics color management, hence I'm asking before I bug Apple with a bug report.

这直接来自模板项目,唯一的非模板代码就是您在下面看到的。请注意,红色,绿色和蓝色都显示正确的组件值,但不显示白色或黑色。

This is straight out of a template project and the only non-template code is what you see below. Note that the red, green, and blue colors all show the correct component values but not white or black.

如果您使用黑色更改订单,问题仍然存在首先是白色,然后是红色,绿色,蓝色。

The problem persists if you change the order with, say, black and white first, then red, green, blue.

这是Swift 1.2和Xcode 6.3.1(6D1002)。

This is Swift 1.2 and Xcode 6.3.1 (6D1002).

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        let colorComps = [
            CGColorGetComponents(UIColor.redColor().CGColor),
            CGColorGetComponents(UIColor.greenColor().CGColor),
            CGColorGetComponents(UIColor.blueColor().CGColor),
            CGColorGetComponents(UIColor.blackColor().CGColor),
            CGColorGetComponents(UIColor.whiteColor().CGColor)
        ]

        var components = [CGFloat]()
        for cc in colorComps
        {
            for i in 0...3 { components.append(cc[i]) }
        }

        println("components: \(components)")
    }
}


推荐答案

你没有理解的是CGColorGetComponents没有任何关于<的任何声明或保证有多少个组件或它们的含义信息包含在颜色的颜色空间中。如果你看一下白色和黑色的颜色空间,你会发现它们都是灰色的形式:

What you're not grasping is that nothing about CGColorGetComponents makes any claims or guarantees about how many components there are or what they mean. That information is contained in the color's color space. If you look at the color space of white and black, you will see they are both forms of grey:

let sp = CGColorGetColorSpace(UIColor.whiteColor().CGColor)
println(sp) // kCGColorSpaceDeviceGrey

因此,它们只用两个值表示:灰色和alpha。

Thus, they are expressed in just two values: the amount of grey, and the alpha.

因此,白色是 [1.0,1.0] - 如果你忽略了你不应该首先阅读的额外两个数字,那就是你得到的。

Hence, white is [1.0,1.0] - which, if you ignore the extra two numbers which you should not have been reading in the first place, is exactly what you got.

同样,黑色是 [0.0,1.0] ,这也是你得到的。

Similarly, black is [0.0,1.0], which is also what you got.

这篇关于CGColorGetComponents()没有返回黑色和白色的正确值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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