5

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.

1

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.