1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#import <AppKit/AppKit.h> #import "OpenGL/gl.h" NSString* hexadecimalValue(NSColor *color) { double redFloatValue, greenFloatValue, blueFloatValue; int redIntValue, greenIntValue, blueIntValue; NSString *redHexValue, *greenHexValue, *blueHexValue; NSColor *convertedColor = [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; if(convertedColor) { [convertedColor getRed:&redFloatValue green:&greenFloatValue blue:&blueFloatValue alpha:NULL]; redIntValue = redFloatValue*255.99999f; greenIntValue = greenFloatValue*255.99999f; blueIntValue = blueFloatValue*255.99999f; redHexValue = [NSString stringWithFormat:@"%02x", redIntValue]; greenHexValue = [NSString stringWithFormat:@"%02x", greenIntValue]; blueHexValue = [NSString stringWithFormat:@"%02x", blueIntValue]; return [NSString stringWithFormat:@"#%@%@%@", redHexValue, greenHexValue, blueHexValue]; } return nil; } void getPiexl(int x, int y){ CGImageRef imageRef = CGDisplayCreateImage(kCGDirectMainDisplay); NSImage *image = [[NSImage alloc]initWithCGImage:imageRef size:NSZeroSize]; NSBitmapImageRep* raw_img = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]]; NSColor* color = [raw_img colorAtX:x y:y]; if (color){ NSString *colorStr = hexadecimalValue(color); printf("%s", [colorStr UTF8String]);#标准输出,给其他程序用 } } |