Constructing an NSColor from Color panel values
I often find myself constructing an NSColor object based on having sampled a color using the standard Color panel. The issue, of course, is that you need to divide the values in the picker by 255 to get the respective red/green/blue components for an RGB NSColor, as those constructors take values in the 0-1 range.
Here’s a trivial category on NSColor to allow you to use the values straight out of the Color panel.
Header:
@interface NSColor (PickerValues)
+ (NSColor *)colorWithCalibratedPickerRed:(float)red green:(float)green
blue:(float)blue alpha:(float)alpha;
@end
Implementation:
@implementation NSColor (PickerValues)
+ (NSColor *)colorWithCalibratedPickerRed:(float)red green:(float)green
blue:(float)blue alpha:(float)alpha
{
float maxVal = 255.0;
return [NSColor colorWithCalibratedRed:(red / maxVal)
green:(green / maxVal)
blue:(blue / maxVal)
alpha:alpha];
}
@end