2
我一直在网上搜索超过几个小时,但没有找到任何东西。检测鼠标下的颜色(Mac)
我想知道如何获得当前鼠标指针所在像素的颜色。 我编写了一个控制台应用程序,所以我没有窗口覆盖或其他东西。
就更多详细信息: 当我建立&运行程序(CMD + R),它应该给我在哪里,我的鼠标指针是目前颜色的控制台日志。那可能吗?
谢谢您的解答!
问候,丹尼尔
PS:我来自德国,只是说(语言错误)
我一直在网上搜索超过几个小时,但没有找到任何东西。检测鼠标下的颜色(Mac)
我想知道如何获得当前鼠标指针所在像素的颜色。 我编写了一个控制台应用程序,所以我没有窗口覆盖或其他东西。
就更多详细信息: 当我建立&运行程序(CMD + R),它应该给我在哪里,我的鼠标指针是目前颜色的控制台日志。那可能吗?
谢谢您的解答!
问候,丹尼尔
PS:我来自德国,只是说(语言错误)
使用this question and answer为出发点,这是一个全功能的命令行程序。您还需要链接到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;
}
如果你想继续运行,你需要采取额外措施,创造并驱动运行循环。
哦,非常感谢你!这正是我正在寻找的!再一次,谢谢你;-)但是有一点,在释放池中释放位图,这是非常必要的吗? – daniel 2012-07-31 19:13:18
这是必要的,因为此示例中的位图不是自动发布的。它已被分配。 @autoreleasepool只消耗自动释放的对象。 – 2012-07-31 19:20:13
对于它的价值,坐标系对我来说并不匹配。我不得不这样做... mouseLoc = {screenHeight} - mouseLoc.y + 20; – whooops 2013-07-16 03:53:23