Skip to content

Instantly share code, notes, and snippets.

@MichaCo
Created May 6, 2015 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save MichaCo/004a529c16b81c7b7c63 to your computer and use it in GitHub Desktop.
Save MichaCo/004a529c16b81c7b7c63 to your computer and use it in GitHub Desktop.
The code transforms color codes which are 3 digits like "#000", 8 digits (with opacity) or named colors to a valid 6 digit hex code, using System.Drawing
public static string ToHexadecimalColorCode(string color)
{
Color baseColor = ColorTranslator.FromHtml(color);
// if its a named color we can use the argb value to transform it to a hex
var argb = baseColor.ToArgb();
return ColorTranslator.ToHtml(Color.FromArgb(argb));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment