Xcode: How to read a pixel off the screen

Discussion in 'Mac Programming' started by ericbreiter, Aug 25, 2012.

  1. ericbreiter macrumors member

    Jun 13, 2012

    I'm working on a little routine in Xcode and I want to read the color off a specific pixel on the screen (e.g. 100,100). I need this to work regardless whether this particular pixel is occupied by my program or something in the background. So if I for instance have something yellow in my application, on the desktop or another application in the background at 100,100 I want to read the yellow color, for instance as RGB.

    I have tried various solutions, but I've come up empty. Any ideas and examples would be greatly appreciated!
  2. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    You should mention what you have tried. That would allow others to either avoid suggesting the same paths, or possibly correcting an assumption you have.

    I did the obvious net search and came up with a hint at StackOverFlow.com;
    Get the color a pixel on the screen in objective-c cocoa app. It looks like half the answer as it didn't describe how you find that pixel location. My Apple doc search was more confusing in that it would take more reading time that I don't have right now.
  3. Member(TM) macrumors member

    May 21, 2011
    To get the device from the global location, CGGetDisplaysWithPoint may be the function to use.

    Assuming CGDisplayCreateImageForRect needs the rectangle parameter in local display space (documentation just says "display space" without specifying local or global), the function CGDisplayBounds may help to convert the coordinates.

Share This Page