ColorTranslator
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.