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.

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • 5
    This answer might point you in the right direction: http://stackoverflow.com/questions/11746471/detect-color-under-mouse-mac – danielbeard Sep 06 '12 at 01:52

1 Answers1

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
iccir
  • 5,078
  • 2
  • 22
  • 34