• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSColor and #xxxxxx format colors
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Follow-Ups:
    • Re: NSColor and #xxxxxx format colors
      • From: Dietrich Epp <email@hidden>
References: 
 >NSColor and #xxxxxx format colors (From: Jan Van Boghout <email@hidden>)

  • Prev by Date: NSColor and #xxxxxx format colors
  • Next by Date: re: (Somewhat OT) What is Image Capture doing?
  • Previous by thread: NSColor and #xxxxxx format colors
  • Next by thread: Re: NSColor and #xxxxxx format colors
  • Index(es):
    • Date
    • Thread