views:

273

answers:

2

Hello,

I am trying to create an application that when the color of a pixel on screen changes to another one (that I know what it is) it does something, doesn't really matter to the question what it does.

Anyway, I am using this:

 CGImageRef window283x420 = CGWindowListCreateImage(NSMakeRect(284, 420, 1, 1), kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
 NSBitmapImageRep *cannonOne = [[NSBitmapImageRep alloc] initWithCGImage:window283x420];

Then, the color I am expecting it to change to here:

NSColor *color = [NSColor colorWithCalibratedRed:0.521569 green:0.380392 blue:0.231373 alpha:1];

And am comparing the colors here:

while (![color isEqualToColor:[cannonOne colorAtX:0 y:0]]) {
 CGImageRef window283x420 = CGWindowListCreateImage(NSMakeRect(284, 420, 1, 1), kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
 NSBitmapImageRep *cannonOne = [[NSBitmapImageRep alloc] initWithCGImage:window283x420];
 if ([color isEqualToColor:[cannonOne colorAtX:0 y:0]]) {

I know the while/if part could be done better, but it should work just fine and not worrying about optimizing the app until it actually works lol.

Now, if for example the color of the pixel doesn't actually change fast (like the color stays like that for a bit) it works perfectly. If it is like a pixel with a color that moves around and reaches 283*420 but doesn't stay there for long (like 0.1s) (as described in the example above) it doesn't work.

The time it stays there shouldn't be a problem, as my app is checking the pixel really fast, as seen in the debug here:

2010-01-04 22:12:58.230 appname[2372:a0f] Checked
2010-01-04 22:12:58.231 appname[2372:a0f] Checked

Any ideas? :)

A: 

It's possible that a moving object never even reaches that point. Your monitor has a update frequency of about 50 hertz, if a single pixel is traveling faster than 50 pixels/sec it won't appear on every pixel!

That is also true for the video buffer because all drawings are double buffered in Mac OS X, updated 50 times per second.

Georg
So, it is not actually a pixel, it's more like an image. If I checked for all the pixels in a vertical line of that image then it could probably work, right? It's a bit late now, and I'm off. Gonna try it as soon as I wake up.
MegaEduX
Yes it could, unless it's still too fast. But your chances are much higher.
Georg
A: 

As gs points out, this is not trivial. You might like to take a look at the Apple CIColorTracking example which uses a custom Core Image filter to detect pixels in a moving image:

http://developer.apple.com/mac/library/samplecode/CIColorTracking/

There's a little more information about how it's done here:

http://parmanoir.com/How_Core_Image_Color_Tracking_works

Rob Keniger