Mega Code Archive

 
Categories / C# / 2D Graphics
 

Converts a color string to a hex value string

//------------------------------------------------------------------------------ // Copyright (c) 2003-2009 Whidsoft Corporation. All Rights Reserved. //------------------------------------------------------------------------------ namespace Whidsoft.WebControls {     using System;     using System.Drawing;     using System.IO;     using System.Collections;     using System.Web.UI;     using System.Web.UI.HtmlControls;     using System.Web.UI.WebControls;     using System.Text.RegularExpressions;     /// <summary>     ///  Utility class with various useful static functions.     /// </summary>     internal class Util     {         /// <summary>         ///  Converts a color string to a hex value string ("Green" -> "#000800")         /// </summary>         internal static string ColorToHexString(string color)         {             if (color[0] == '#')             {                 return color;             }             Color c = ColorTranslator.FromHtml(color);             return ColorToHexString(c);         }         /// <summary>         ///  Converts a Color to a hex value string (Color.Green -> "#000800")         /// </summary>         internal static string ColorToHexString(Color c)         {             string r = Convert.ToString(c.R, 16);             if (r.Length < 2)                 r = "0" + r;             string g = Convert.ToString(c.G, 16);             if (g.Length < 2)                 g = "0" + g;             string b = Convert.ToString(c.B, 16);             if (b.Length < 2)                 b = "0" + b;             string str = "#" + r + g + b;             return str;         }     } }