Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ColorTranslator.ToHtml() return string issue

Tags:

c#

I need the hexadecimal string of color so I am using ColorTranslator.ToHtml() property which returns string hex of Color.

If I choose a random color it returns "#FFF0B6" etc. However, if I choose a system-defined color for example Color.Black it returns "Black" in string.

I need hexadecimal color codes in string whether they are defined in system or not. Any suggestions?

like image 890
cihadakt Avatar asked Feb 05 '13 15:02

cihadakt


1 Answers

I have found this extension method worked well for me:

public static string ToHexValue(this Color color)
{
   return "#" + color.R.ToString("X2") + 
                color.G.ToString("X2") + 
                color.B.ToString("X2");
}

According to MSDN, that is indeed what ColorTranslator.ToHtml() is intended to do.

like image 63
Arran Avatar answered Oct 15 '22 04:10

Arran