• 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: HTML hexidecimal code from NSColor
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: HTML hexidecimal code from NSColor


  • Subject: RE: HTML hexidecimal code from NSColor
  • From: Michael Kelly <email@hidden>
  • Date: Fri, 16 May 2003 10:46:14 -0400

And use @"XXX" as your printf format, so you get two exactly
characters per RGB component.

-----Original Message-----
From: Clark S. Cox III [mailto:email@hidden]
Sent: Friday, May 16, 2003 8:32 AM
To: Francisco Tolmasky
Cc: email@hidden
Subject: Re: HTML hexidecimal code from NSColor

On Thursday, May 15, 2003, at 22:51 US/Eastern, Francisco Tolmasky
wrote:

> Given an NSColor object, how do I create a string with the HTML
> hexadecimal color codes?
>
> I tried this:
>
> - (NSString *)colorString
> {
> float red, green, blue;
> [[color colorUsingColorSpaceName: NSCalibratedRGBColorSpace]
> getRed: &red green: &green blue: &blue alpha: nil];
> return [NSString stringWithFormat: @"%X%X%X", red, green, blue];
> }
>
> This doesn't work though, since the numbers returned are between 0.0
> and 1.0, not 0 and 255. Does anyone know how to get it to work?

Well, if you have a number between 0 and 1, and you want it to be
between 0 and 255, then just multiply by 255. Also, you should never
pass a float to printf-like functions/methods via %X. Try this:



- (NSString *)colorString
{
float red, green, blue;
[[color colorUsingColorSpaceName: NSCalibratedRGBColorSpace]
getRed: &red green: &green blue: &blue alpha: nil];

return [NSString stringWithFormat: @"%X%X%X", (unsigned)(red*255),
(unsigned)(green*255), (unsigned)(blue*255)];
}




--
http://homepage.mac.com/clarkcox3/
email@hidden
Clark S. Cox, III
_______________________________________________
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.
_______________________________________________
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.

  • Prev by Date: NSImage method problem
  • Next by Date: Re: NSImage method problem
  • Previous by thread: Re: HTML hexidecimal code from NSColor
  • Next by thread: printing over-optimizing ?
  • Index(es):
    • Date
    • Thread