Re: NSColor and #xxxxxx format colors
Re: NSColor and #xxxxxx format colors
- Subject: Re: NSColor and #xxxxxx format colors
- From: Dietrich Epp <email@hidden>
- Date: Thu, 26 Dec 2002 14:14:20 -0800
Whoops, forgot to send to the list...
On Thursday, December 26, 2002, at 06:18 , David Remahl wrote:
First split the color string up into its components. ( r = FF, g = FF,
b = FF ).
Then convert each of the two digit hexadecimal strings to decimal
unsigned integers between 0 and 255. Study base conversion or look up
some sample code.
r = 255
g = 255
b = 255
Then you create the NSColor like this:
[NSColor colorWithRed:255.0 / r green: 255.0 / g blue:255.0 / b];
This could all be placed in a category for reuse:
- (NSColor *)colorWithHTMLColorString:(NSString *)inStr
{
// implementation of the above
}
The other way around is achived simply by reversing the algorithm.
There are methods on NSColor for getting the red, green and blue
components, and then you convert them to two digit hex (easily done
with printf / NSString stringWithFormat) and concatenate the components
together with a #.
Just might want to add that it's a good idea to convert it to RGB space
first. The docs say "it's illegal to ask an NSColor for components that
aren't defined for its color space", see
file:///Developer/Documentation/Cocoa/TasksAndConcepts/ProgrammingTopics/DrawColor/
Tasks/ColorComponents.html
NSColor* aColor;
float r, g, b, a;
char buffer[8];
[[aColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace]
getRed:&r greed:&g blue:&b alpha:&a]
snprintf (buffer, sizeof (buffer), "#X...
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.