Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DigitalColor Meter logic - how does it do it?

So, this basically what I'm curious about :

  • How does this little tool get the area (and the specific pixel) below the mouse?
  • How does it then analyze the RGB values of that selected point?

Any ideas? Pointing me to the right direction would also be welcome.


Hint : I would be more interested in a native Objective-C/Cocoa approach.

like image 584
Dr.Kameleon Avatar asked Nov 04 '22 16:11

Dr.Kameleon


1 Answers

Basically, DigitalColor Meter gets the mouse coordinates, takes a CGImageRef screenshot around that area and then accesses the raw pixel data to calculate the RGB value.

You can discover which APIs an application calls by using the nm command. In this case:

nm /Applications/Utilities/DigitalColor\ Meter.app/Contents/MacOS/DigitalColor\ Meter

Which reveals some interesting calls:

U _CGDisplayBounds
U _CGGetDisplaysWithPoint
U _CGSCaptureWindowsContentsToRectWithOptions
U _CGSCurrentInputPointerPosition
U _CGSGetOnScreenWindowCount
U _CGSGetOnScreenWindowList

The CGS* routines are private SPI - on the bright side, there is a public API equivalent called CGWindowListCreateImage()

Once you have a CGImageRef, you can access the raw pixel data using:

CGImageGetDataProvider
CGDataProviderCopyData
like image 195
iccir Avatar answered Nov 13 '22 17:11

iccir