Quick Tip: Color objects from Hex Codes in .Net

Published Mar 28, 2011 (13 years ago)
Danger icon
The last modifications of this post were around 13 years ago, some information may be outdated!

Hex Palette .Net provides a wonderful class called the color class. There are plenty of predefined colors in it that make it easy when you want to set the color of a label or background quickly and easily. Sometimes though, you need to have a little more control on the color you're working with. Having dabbled in the web realm for so long, hex codes are my friend. So when I discovered I couldn't simply set the ForeColor property of my label webcontrol using Hex values. I was a bit frustrated and stumped.

Fortunately, the Color class itself came to the rescue! They provide a translator class that allows you to easily work hex code into your labels. To start, simply import the System.Drawing library into your class. From there, do something like this:

lblAppName.ForeColor = ColorTranslator.FromHtml("#DEDBEF");

You'll get a nice shade of light blue, err, off white, err, periwinkle using that code above. What's really nice is that the ColorTranslator is a shared class, so you don't even need to instantiate an instance of the object to use it. Enjoy!