在objective-c可可应用程序中获取屏幕上的颜色像素 [英] Get the color a pixel on the screen in objective-c cocoa app

查看:352
本文介绍了在objective-c可可应用程序中获取屏幕上的颜色像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在objective-c中创建一个颜色选择器可可应用程序,这样我可以得到我屏幕上任何像素的颜色。有没有办法得到一个屏幕像素在objective-c?

I am trying to create a color picker cocoa app in objective-c so I can get the color of any pixel on my screen. Is there a way to get the color a certain screen pixel in objective-c?

推荐答案

你可以使用CGDisplayCreateImageForRect获得一个CGImageRef包含你感兴趣的点。一旦你有一个CGImage,你可以通过渲染图像到自定义缓冲区,或使用NSBitmapImageRep的initWithCGImage然后colorAtX:y:得到一个颜色值。 NSBitmapImageRep方法的堆栈溢出代码窗口写入代码如下:

You can use CGDisplayCreateImageForRect to get a CGImageRef that encompasses the point you're interested in. Once you have a CGImage, you can get a color value out of it by rendering the image into custom buffer, or by using NSBitmapImageRep's initWithCGImage and then colorAtX:y:. The "written in stack overflow code window" code for the NSBitmapImageRep method would look something like:

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];
}

这可能会返回设备颜色空间中的颜色。可能有用的是返回校准的RBG颜色空间中的颜色。

That will probably return the color in device color space. It might be useful to return the color in calibrated RBG color space instead.

这篇关于在objective-c可可应用程序中获取屏幕上的颜色像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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