我需要从屏幕上的像素获取颜色并转换其颜色空间。我遇到的问题是,将值与数字色度计应用程序进行比较时,颜色值不相同。
// create a 1x1 image at the mouse position
if let image:CGImage = CGDisplayCreateImage(disID, rect: CGRect(x: x, y: y, width: 1, height: 1))
{
let bitmap = NSBitmapImageRep(cgImage: image)
// get the color from the bitmap and convert its colorspace to sRGB
var color = bitmap.colorAt(x: 0, y: 0)!
color = color.usingColorSpace(.sRGB)!
// print the RGB values
let red = color.redComponent, green = color.greenComponent, blue = color.blueComponent
print("r:", Int(red * 255), " g:", Int(green * 255), " b:", Int(blue * 255))
}
我的代码(转换为 sRGB):255, 38, 0
数字色度计 (sRGB):255, 4, 0
如何从屏幕上的像素中获取具有正确色彩空间值的颜色?
Update:
如果您不将颜色空间转换为任何内容(或将其转换为校准RGB),则当设置为“显示本机值”时,这些值将与数字色度计的值相匹配。
我的代码(未转换):255, 0, 1
数字色度计(设置为:显示原始值):255, 0, 1
那么,为什么当颜色值与 DCM 应用中的原生值匹配时,将颜色转换为 sRGB 并将其与 DCM 的值(以 sRGB 为单位)进行比较却不匹配?我也尝试过转换为其他色彩空间,但总是与 DCM 不同。