Cambia Research - Supporting the Microsoft .NET Developer Community Supporting the Microsoft .NET Developer Community  

     | Home  | Articles  | Categories  | Coders  | Search  | Submit  | Contact Us    
There are two ways to write error-free programs; only the third works. --Alan J. Perlis

Share Your Knowledge! -- Create and submit your articles the easy way with WebWriter.

Updated:06:35 PM CT Jan 09, 2007
Posted:05:45 PM CT Jan 09, 2007

Convert .NET Color to Hex String

Author: Steve Lautenschlager

SnippetC#.NETCSSHTML

 Summary

For a long time I had a fairly complicated way of converting the .NET Color object to a string such as #F782AB. Since then some of you have informed me of a much easier way. I'll show you the easy way first and then include the longer method below.
Color c = Color.Red;
string strColor = System.Drawing.ColorTranslator.ToHtml(c);
Pretty simple isn't it. Don't you just love these little .NET niceties that you can never find when you want 'em?

So before I figured that out, it worked something like this. Note that lblOutput is a Label control on my ASP.NET page.

Example: How to use the conversion method

public void TestColorToHexString()
{
   Color c1 = Color.White;
   Color c2 = Color.Black;
   Color c3 = Color.LightGoldenrodYellow;
   Color c4 = Color.Gold;

   lblOutput.Text = c1.Name + " =  " 
      + Snippets00002.ColorToHexString(c1) + "<br>";
   lblOutput.Text += c2.Name + " =  " 
      + Snippets00002.ColorToHexString(c2) + "<br>";
   lblOutput.Text += c3.Name + " =  " 
      + Snippets00002.ColorToHexString(c3) + "<br>";
   lblOutput.Text += c4.Name + " =  " 
      + Snippets00002.ColorToHexString(c4) + "<br>";

}
If you run the previous method you'll get the following output displayed in the Label control on the aspx page.

Example: Output

White = FFFFFF
Black = 000000
LightGoldenrodYellow = FAFAD2
Gold = FFD700
Here's the class:

The C# Class:

namespace Cambia.CoreLib
{
   using System;
   using System.Drawing;

   /// <summary>
   /// Useful C# snippets from CambiaResearch.com
   /// </summary>
   public class Snippets00002
   {
      #region -- Data Members --
      static char[] hexDigits = {
         '0', '1', '2', '3', '4', '5', '6', '7',
         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
      #endregion

      public Snippets00002()
      {
      }

      /// <summary>
      /// Convert a .NET Color to a hex string.
      /// </summary>
      /// <returns>ex: "FFFFFF", "AB12E9"</returns>
      public static string ColorToHexString(Color color) 
      {
         byte[] bytes = new byte[3];
         bytes[0] = color.R;
         bytes[1] = color.G;
         bytes[2] = color.B;
         char[] chars = new char[bytes.Length * 2];
         for (int i = 0; i < bytes.Length; i++) 
         {
            int b = bytes[i];
            chars[i * 2] = hexDigits[b >> 4];
            chars[i * 2 + 1] = hexDigits[b & 0xF];
         }
         return new string(chars);
      }


   }
}

Add New Comment
Convert .NET Color to Hex String
Szymon22 Mar 07, 10:43Reply 
re: Convert .NET Color to Hex String
Steve Lautenschlager22 Mar 07, 12:20Reply 
re: Convert .NET Color to Hex String
Bee26 Mar 07, 22:04Reply 
re: Convert .NET Color to Hex String
Steve Lautenschlager26 Mar 07, 22:08Reply 
re: Convert .NET Color to Hex String
Brian31 Oct 07, 11:29Reply 
re: Convert .NET Color to Hex String
Brian31 Oct 07, 13:09Reply 
Convert .NET Color to Hex String
Baal17 Mar 08, 14:04Reply 
re: Convert .NET Color to Hex String
Baal17 Mar 08, 14:13Reply 
re: Convert .NET Color to Hex String
Baal17 Mar 08, 14:34Reply 
CR Comments by Cambia Research
advertisement
 
Steve Lautenschlager (steve)
Steve is the founder and creator of Cambia Research. Developing and maintaining the site combines his passions for technology, writing and education.
Steve holds a Ph.D. in particle physics from Duke University, has worked at CERN, the European center for particle physics (where the web was born) and in Microsoft's web division with microsoft.com, msnbc.com and other web properties. Steve is a web consultant specializing in Microsoft.NET technologies. Read more here.


 
Copyright © Cambia Research 2002-2007. All Rights Reserved. steve [ at ] cambiaresearch.com