Saturday, April 19, 2014

Get color from HTML color string

Here is a quick code snippet to get color from HTML color string.

private Color GetColorFromString(string inStringColorCode)
{

Color togoColor = Colors.Transparent;
try
{

byte alpha = Convert.ToByte(Convert.ToInt32(inStringColorCode.Substring(0, 2), 16));

byte red = Convert.ToByte(Convert.ToInt32(inStringColorCode.Substring(2, 2), 16));

byte green = Convert.ToByte(Convert.ToInt32(inStringColorCode.Substring(4, 2), 16));

byte blue = Convert.ToByte(Convert.ToInt32(inStringColorCode.Substring(6, 2), 16));

togoColor = Color.FromArgb(alpha, red, green, blue);

}

catch (Exception ex)
{

MessageBox.Show("Error when converting color ARGB code " + inStringColorCode + ". It must be of length 8 characters." + ex.ToString(), "Error", MessageBoxButton.OK);
}
return togoColor;
}

Happy Coding !

No comments:

Post a Comment

All Blogs so far ...