ColorTranslator

Published 26 October 04 11:24 AM | despos

Please raise hand who knows about a .NET class named ColorTranslator. I've been looking for a similar class for too long and now the excellent Lutz Roeder's .NET Reflector revealed that it exists. And it's there since the beginning of the .NET Framework.

ColorTranslator is a fairly simple, sealed class that acts as a utility tool to convert between the various formats of colors: HTML, OLE, Win32, system and so on. It proves useful (at least from my perspective) to control developers especially when one needs to accept a color through a System.Drawing.Color property and then render it back to as a HTML string. This normally happens when you need to set a Style color attribute.  

You can't just use the ToString method on Color. The string you get in this way is a description of the color but nothing that a browser can recognize. You have better luck if you opt for the Name property of the same System.Drawing.Color class. As the property name suggests, though, in this case you get the name of the color which turns out to be a "known" color like Lavender, Purple, Cyan, and so on. What if you express the color through its base R-G-B components? For example, imagine you get a color as follows:

Color c = Color.FromName("#112233");

Next, you need to render the color back to its HTML string: #112233. Try it.

You have to build the string manually. You first check the color against the list of known colors and if it's not there you build the string by prefixing R-G-B components with a # symbol. Just what ColorTranslator does free of charge for you. 

string htmlColor = ColorTranslator.ToHTML(c);

Easy and effective.

 

Comments

# TrackBack said on October 26, 2004 05:00 AM:
# TrackBack said on October 26, 2004 05:01 AM:
# Derek Lakin said on October 26, 2004 05:58 AM:

[raises hand] Last time I looked, the Color class had a few bugs to do with Hue, Saturation and Brightness, but it doesn't sound like that affects what you're doing.

# asdf said on October 26, 2004 06:42 AM:

Last time I checked, there was no need nor purpose to put color values in HTML. Use CSS!

# Roy Tore Gurskevik said on October 26, 2004 08:06 AM:

Yes, I know. This is a well hidden feature . I used quite a lot of time before I found it.

# Jeff Parker said on October 26, 2004 08:44 AM:

There are a lot of strange, hidden and wonderful things hidden down deep in System.Drawing You mention of of the cooler things which yes I knew about but I agree using CSS is better, but Dynamic CSS which I have done is even more fun and I found this useful for that before.

One of the stranger things is the DontCare Enumeration.
System.Drawing.Imaging.PixelFormat.DontCare

# DinoE said on October 26, 2004 09:18 AM:

Using CSS would be fine if the goal is serving a page styled in a certain way. The reason I was looking for a class like ColorTranslator is for control development. When you have a "custom" color property like RolloverColor for a custom context menu, you need to be able to translate that value to a HTML color string. And dynamically. Sure, you can always add a CssClass property (btw, guess it is defined on WebControl already) but having that doesn't save you from providing an alternative way to set colors that is more VS-ish.

Do you feel I'm missing something here?

# Wessam Zeidan said on October 26, 2004 10:19 AM:

I've used this class in a custom control I made. I also found it with reflector.

# Wes said on October 26, 2004 10:35 AM:

Cool, I didn't know about this, I have written my own functions to do this same thing. Thanks for the Tip.

# TrackBack said on October 26, 2004 04:31 PM:
# TrackBack said on November 5, 2004 03:50 AM:
# .Net Adventures said on November 5, 2004 09:55 AM:

Trackback

# joe said on November 19, 2004 06:09 AM:

Bonjour,
I don't speack english very weel. I'm sorry :!
Votre site est intéressant. Je le trouve ergonomique, facile à sufer et agréable à lire. La page prend un peu de temp à s'afficher sur mon pc mais sa dois etre ma connection.
Bonne continuation - N'hésitez pas à visiter mon site www.mes-vacances.com

Cordialement,
Jonathan

# TrackBack said on November 25, 2004 06:47 AM:
# TrackBack said on December 12, 2004 10:40 PM:
# TrackBack said on December 12, 2004 10:42 PM:
# TrackBack said on January 17, 2005 11:10 AM:
# TrackBack said on April 4, 2005 12:38 PM:
# Gdsffsd said on May 25, 2007 08:24 AM:

<a href="extremedrawing.tripod.com/">adult comics</a>

# crazy said on August 5, 2007 04:24 PM:

<a href=" iiadv.biz/.../acceptance-indemnity-insurance.html ">acceptance indemnity insurance</a> <a href=" sscoll.info/.../is-loan-consolidation.html ">is loan consolidation</a> <a href=" ddcoll.info/.../transfer-balance-credit-cards.html ">transfer balance credit cards</a> <a href=" iiadv.biz/.../american-standard-insurance.html ">american standard insurance</a> <a href=" ddcoll.info/.../tax-return-loan.html ">tax return loan</a>

# John said on October 1, 2007 07:24 PM:

Hi, nice page.!

look on my pages:

<a href="profitb.free-site-host.com/.../associate-complaint-international-profit.html">associate complaint international profit</a>

ok.. bitte  

# Aineias said on November 13, 2007 11:53 PM:

Cool.

[URL=ford-obd.cadillactechnology.cn#">ford-obd.cadillactechnology.cn] ford obd [/URL]

ford-obd.cadillactechnology.cn ford obd

# aostzbpwvy said on May 4, 2008 12:57 PM:

Wow, cool man, big thanks! <a href=http://lgxvtttvbrcgsz.com >http://lgxvtttvbrcgsz.com</a>

# ColorTranslator | devintelligence.com said on July 20, 2008 09:16 AM:

Pingback from  ColorTranslator | devintelligence.com

Leave a Comment

(required) 
(required) 
(optional)
(required)