Detect color under mouse (Mac)

I’ve searched the Internet for over an hour and haven’t found anything.

I want to know how to get the color of the pixel where the mouse pointer is. I programmed a console application, so I don’t have an overlay window or anything else.

More: When I create and run the program (cmd + r), it should provide me with a console log of the color in which my mouse pointer is located. Is it possible?

Thank you for your responses!

Cheers, Daniel

PS: I'm from Germany, I just speak (language errors)

+2
source share
1 answer

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 continue working, you need to take additional steps to create and run a run loop.

+8
source

Source: https://habr.com/ru/post/1432617/


All Articles