How do I create a NSColor from a RGB value?
15
votes
4 Answers
17
votes
Per the NSColor documentation:
NSColor *myColor = [NSColor colorWithCalibratedRed:redValue green:greenValue blue:blueValue alpha:1.0f];
11
votes
Also don't forget to do the following conversion from the actual RGB values you get, lets say from Photoshop...
an RGB of (226, 226, 226) could be instantiated as a NSColor using the values:
Red: 226/255 = 0.886...
Green: 226/255 = 0.886...
Blue: 226/255 = 0.886...
[NSColor colorWithDeviceRed:0.886f green:0.886f blue:0.886f alpha:1.0f];
Why 255? 8-bit color channels range from 0 to 255 (inclusive). When normalized this is scaled to the range [0,1] (inclusive). See references for conversions from normalized values to unnormalized values and vice versa.
References
- OpenGL ES Full Specification, See Table 2.7 on Page 38
- Unreal Engine Color Conversion
4
votes