Skip navigation.
 
mlRe: NSColor question
FROM : Todd Yandell
DATE : Mon Apr 25 19:11:12 2005

Hi,

Here's a simple category on NSColor that might help you:

/* Examples:
   
   NSColor *red = [NSColor colorWithRGBString:@"255, 0, 0"];
   NSColor *yellow = [NSColor colorWithRGBAString:@"255,255,0,128"];
   
*/

@implementation NSColor ( RGBStringAdditions )

+ (NSColor *)colorWithRGBString:(NSString *)string
{
   NSArray *channels = [string componentsSeparatedByString:@","];
   NSColor *color = [NSColor colorWithDeviceRed:
       [[channels objectAtIndex:0] floatValue] / 255.0
       green:[[channels objectAtIndex:1] floatValue] / 255.0
       blue:[[channels objectAtIndex:2] floatValue] / 255.0
       alpha:1.0];
   return color;
}

+ (NSColor *)colorWithRGBAString:(NSString *)string
{
   NSArray *channels = [string componentsSeparatedByString:@","];
   NSColor *color = [NSColor colorWithDeviceRed:
       [[channels objectAtIndex:0] floatValue] / 255.0
       green:[[channels objectAtIndex:1] floatValue] / 255.0
       blue:[[channels objectAtIndex:2] floatValue] / 255.0
       alpha:[[channels objectAtIndex:3] floatValue] / 255.0];
   return color;
}

@end

Todd Yandell

Related mailsAuthorDate
mlNSColor question Luc Vandal Apr 25, 18:04
mlRe: NSColor question James Housley Apr 25, 18:12
mlRe: NSColor question Nicko van Someren Apr 25, 18:15
mlRe: NSColor question Stephane Sudre Apr 25, 18:19
mlRe: NSColor question Todd Yandell Apr 25, 19:11