Skip to content

Instantly share code, notes, and snippets.

@atr000
Created February 8, 2010 05:17
Show Gist options
  • Save atr000/297903 to your computer and use it in GitHub Desktop.
Save atr000/297903 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
if (argc != 4) {
printf("Usage: pixelcolor [imgpath] [pixelposx] [pixelposy]\n");
return 1;
}
[NSApplication sharedApplication];
NSArray *args = [[NSProcessInfo processInfo] arguments];
NSString *filePath = [args objectAtIndex:1];
NSString *stringPosX = [args objectAtIndex:2];
float pixelPosX = [stringPosX floatValue];
NSString *stringPosY = [args objectAtIndex:3];
float pixelPosY = [stringPosY floatValue];
NSImage *img = [[NSImage alloc] initWithContentsOfFile:filePath];
if (img == nil) {
printf("Error: Could not load image file.\n");
return 1;
}
NSPoint point = NSMakePoint(pixelPosX, pixelPosY);
[img lockFocus];
NSColor *color = NSReadPixel(point);
[img unlockFocus];
[img release];
float red, green, blue, alpha;
[color getRed:&red green:&green blue:&blue alpha:&alpha];
printf("%f %f %f %f\n", red, green, blue, alpha);
[pool drain];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment