views:

671

answers:

2

How do I create a NSColor from a RGB value?

+6  A: 

Per the NSColor documentation:

NSColor *myColor = [NSColor colorWithCalibratedRed:redValue green:greenValue blue:blueValue alpha:1.0f];
Matt Ball
You should use 1.0f instead of 1.0, as 1.0 is a double, which is not a native type for the iPhone.
nash
You are correct — the perils of typing code quickly!
Matt Ball
It's probably worth making it clear that the color values are from 0.0 to 1.0.Also, the "calibrated" version is indeed more than likely the correct variant of this method to use. You want to use the NSCalibratedRGBColorSpace (which corresponds to [NSColorSpace genericRGBColorSpace]) in all internal dealings with colors. And only use the "device" color spaces variants when outputting to a known specific device.
Kelan
A: 
float red = 0.5f;
float green = 0.2f;
float blue = 0.4f;
float alpha = 0.8f;

NSColor rgb = [NSColor colorWithDeviceRed: red green: green blue: blue alpha: alpha]
nash