In most programming languages, such as .NET or Java, if you wanted to create a Color object from an RGB, you would execute some method on a Color class passing in Red, Green and Blue values where these values are typically within the range 0 through 255.
If you want to do perform a similar operation in Cocoa, i.e. creating a NSColor object, you need to pass in color values within the range 0 through 1.
Typically, to create a NSColor object in Cocoa, you would use a method such as
[NSColor colorWithDeviceRed: green: blue: alpha:]
To convert RGB values into the appropriate values for this method, you need to add 1 to each separate RGB value and then divide by 256.
So, for example, Alice Blue with an RGB of (240, 248, 255) could be instantiated as a NSColor using the values:
Red: (240+1)/256 = 0.941
Green: (248+1)/256 = 0.973
Blue: (255+1)/256 = 1.0
[NSColor colorWithDeviceRed:0.941 green:0.973 blue:1.0 alpha:1.0] 
Jerry Udensi
4/27/2017 09:21:23 am

Nice. Was having troubles using NSColor with RGB values till I found this.


Leave a Reply.