Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

ios - CGColorGetComponents() not returning the correct values for black and for white

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.

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)")
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...