Sar*_*sen 5 macos cocoa pixel colors objective-c
我正在尝试在objective-c中创建一个颜色选择器可可应用程序,这样我就可以获得屏幕上任何像素的颜色.有没有办法在Objective-c中获得某个屏幕像素的颜色?
Jon*_*ess 14
您可以使用CGDisplayCreateImageForRect获得涵盖你所感兴趣的点CGImageRef一旦你有一个CGImage,你可以得到一个颜色值超出它的渲染图像到自定义缓存,或使用NSBitmapImageRep的initWithCGImage然后colorAtX: ÿ:.NSBitmapImageRep方法的"写入堆栈溢出代码窗口"代码如下所示:
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];
}
Run Code Online (Sandbox Code Playgroud)
这可能会返回设备颜色空间中的颜色.改为在校准的RBG颜色空间中返回颜色可能很有用.
| 归档时间: |
|
| 查看次数: |
7628 次 |
| 最近记录: |