Wesley Wesley - 16 days ago 6x
Objective-C Question

Convert NSColor to RGB

I'm trying to convert an NSColor to RGB, but it seems to give an entirely incorrect result:

NSColor *testColor = [NSColor colorWithCalibratedWhite:0.65 alpha:1.0];

const CGFloat* components = CGColorGetComponents(testColor.CGColor);
NSLog(@"Red: %f", components[0]);
NSLog(@"Green: %f", components[1]);
NSLog(@"Blue: %f", components[2]);
NSLog(@"Alpha: %f", CGColorGetAlpha(testColor.CGColor));

I get back : red = 0.65 - green = 1.0 - blue = 0.0 and alpha is 1.0 - which results in an entirely different color. (It should be gray, now it's green).

Am I doing something wrong?


You need to convert the color to an RGB color space using an NSColorSpace object first, then you can get the components using the various NSColor accessor methods