On a couple of occasions I’ve now had to create HTML colors from Windows RGB colors. Why you might ask? I have a couple of WinForms that are using the Web Browser control and I needed to match the colors of the theme into the HTML that gets dynamically created and rendered.
There are a couple of ways to do this:
string HeaderColor = "#" +
SystemColors.InactiveCaption.ToArgb().ToString("x").Substring(2);
The above is reliable for all situations. If you are using Web Colors on your Windows Forms you can get their names like this:
string Color = cd.Color.ToKnownColor().ToString();
Note that this basically returns the name of the color in the same way you see it in the IDE (in fact this is likely what the IDE uses to let you select the colors). This means if you use a non-Web color like the InactiveCaption color you get the name of the color ie. InactiveCaption. But if you choose something like BlueSteel the value will be returned as BlueSteel which works both in WinForms and the Web.
Other Posts you might also like