Pierre Greborio.NET

Talking about .NET world

Is ColorTranslator.ToHtml correct ?

I was playing with colors with transparency when I had to use System.Drawing.ColorTranslator.ToHtml to translate the ARGB color into HTML string color representation. I discovered that the above method doesn't consider the alpha channel. Then, I created my own:

private string ToHtmlAlpha(System.Drawing.Color c)
{
 return String.Concat("#",
    c.A.ToString("X2", null),
    c.R.ToString("X2", null),
    c.G.ToString("X2", null),
    c.B.ToString("X2", null));
}

Posted: Mar 10 2004, 12:30 PM by PierreG | with 2 comment(s)
Filed under:

Comments

Raymond Chen said:

Um, HTML colors don't support alpha, at least not as far as I can tell. The HTML4 spec says that colors are in sRGB, not ARGB.
# March 10, 2004 10:11 AM

Pierre Greborio said:

If you use it as a response for a web service and then into a chart you need alpha too, for transparency.
# March 10, 2004 10:16 AM