我需要从屏幕上的像素中获取颜色并转换其颜色空间。我的问题是,当将这些值与Digital Color Meter应用程序进行比较时,它们的颜色值不同。
// 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
如何从屏幕上的像素获取具有正确颜色空间值的颜色?
更新:
如果您不将颜色空间转换为任何内容(或将其转换为calibratedRGB),则在Digital Color Meter设置为“显示本地值”时,值与Digital Color Meters值匹配。
我的代码(未转换):255, 0, 1
数字色彩计(设置为:显示本地值):
255, 0, 1
那么,当颜色值与DCM应用程序中的本机值匹配时,将颜色转换为sRGB并将其与DCM的值(在sRGB中)进行比较时,为什么它们不匹配?我还尝试将其转换为其他颜色空间,但始终与DCM中的值不同。