You can use CGDisplayCreateImageForRect to get a CGImageRef that spans the point of interest to you. After you have CGImage, you can get the color value from it by providing the image in a custom buffer or using NSBitmapImageRep initWithCGImage and then colorAtX: y :. The code "written on the code overflow code stack" for the NSBitmapImageRep method will look something like this:
NSColor *MyColorAtScreenCoordinate(CGDirectDisplayID displayID, NSInteger x, NSInteger y) {
CGImageRef image = CGDisplayCreateImageForRect(displayID, CGRectMake(x, y, 1, 1));
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:image];
CGImageRelease(image);
NSColor *color = [bitmap colorAtX:0 y:0];
[bitmap release];
}
This will likely return the color in the color space of the device. It might be useful to return the color to the calibrated RBG color space.
source
share