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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With