DigitalColor Meter Logic - How Does It Do It?

So this is basically what I am interested in:

  • How does this small tool get the area (and a specific pixel) under the mouse?
  • How does it analyze the RGB values ​​for the selected point?

Any ideas? It is also advisable to indicate the right direction.


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

+4
source share
1 answer

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

You can find out which API applications the application calls using the nm command. In this case:

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

This shows some interesting challenges:

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

CGS * routines are private SPIs - 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 
+1
source

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


All Articles