在鼠标下检测颜色(Mac) [英] Detect color under mouse (Mac)
问题描述
我一直在网上搜索了几个小时,没有找到任何东西。
i've been searching the web for more than hours and didn't find anything.
我想知道如何获得像素的颜色鼠标指针目前是。
我编程了一个控制台应用程序,所以我没有重叠的窗口或其他东西。
I want to know how to get the color of the pixel where the mouse pointer currently is. I programmed a console application, so I have no window to overlay or something else.
更多细节:
当我构建和运行程序(cmd + r),它应该给我一个控制台日志的颜色,我的鼠标指针目前是。这是可能吗?
Just more detail: When I build&run the program ( cmd+r ), it should give me a console log of the color where my mouse pointer currently is. Is that possible?
谢谢您的答案!
问候,丹尼尔
PS:我来自德国,只是说(语言错误)
PS: I'm from germany, just saying (language mistakes)
推荐答案
href =http://stackoverflow.com/questions/4395420/get-the-color-a-pixel-on-the-screen-in-objective-c-cocoa-app>此问题和答案作为起点,这是一个功能齐全的命令行程序。您还需要链接到Cocoa Framework。
Using this question and answer as a starting point, this is a fully functional command line program. You also need to link to the Cocoa Framework.
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Grab the current mouse location.
NSPoint mouseLoc = [NSEvent mouseLocation];
// Grab the display for said mouse location.
uint32_t count = 0;
CGDirectDisplayID displayForPoint;
if (CGGetDisplaysWithPoint(NSPointToCGPoint(mouseLoc), 1, &displayForPoint, &count) != kCGErrorSuccess)
{
NSLog(@"Oops.");
return 0;
}
// Grab the color on said display at said mouse location.
CGImageRef image = CGDisplayCreateImageForRect(displayForPoint, CGRectMake(mouseLoc.x, mouseLoc.y, 1, 1));
NSBitmapImageRep* bitmap = [[NSBitmapImageRep alloc] initWithCGImage:image];
CGImageRelease(image);
NSColor* color = [bitmap colorAtX:0 y:0];
NSLog(@"%@", color);
[bitmap release];
}
return 0;
}
如果要继续运行,您需要采取其他措施创建和驱动运行循环。
If you want to to keep running you'll need to take additional measures to create and drive a run loop.
这篇关于在鼠标下检测颜色(Mac)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!