objective-cmacoscocoanscolor

Getting a hex value from NSColor in Cocoa?


Does anyone know if it's possible to get a hexadecimal value of the color from NSColor in Cocoa?


Solution

  • Example using blue NSColor:

    NSColor* color = [NSColor blueColor];
    
    NSString* hexString = [NSString stringWithFormat:@"%02X%02X%02X", 
    (int) (color.redComponent * 0xFF), (int) (color.greenComponent * 0xFF), 
    (int) (color.blueComponent * 0xFF)];