Re: NSColor and #xxxxxx format colors
Re: NSColor and #xxxxxx format colors
- Subject: Re: NSColor and #xxxxxx format colors
- From: David Remahl <email@hidden>
- Date: Thu, 26 Dec 2002 15:18:39 +0100
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 #.
/ Regards, David Remahl
How would I convert the colors used in html (like #FFFFFF for white,
etc) to an NSColor and the other way around? It doesn't seem to be
included in the standard NSColor.
Thanks,
Jan Van Boghout
_______________________________________________
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.