Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a NSColor with RGB value

How do I create a NSColor from a RGB value?

like image 690
nanochrome Avatar asked Dec 21 '09 05:12

nanochrome


3 Answers

Per the NSColor documentation:

NSColor *myColor = [NSColor colorWithCalibratedRed:redValue green:greenValue blue:blueValue alpha:1.0f];
like image 181
Matt Ball Avatar answered Nov 07 '22 03:11

Matt Ball


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
like image 31
MiMo Avatar answered Nov 07 '22 05:11

MiMo


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];
like image 23
nash Avatar answered Nov 07 '22 05:11

nash