Skip to content

Instantly share code, notes, and snippets.

@iletai
Created February 19, 2021 07:25
Show Gist options
  • Save iletai/dbc4c6ab817ca9ab5785d4b46e300fdd to your computer and use it in GitHub Desktop.
Save iletai/dbc4c6ab817ca9ab5785d4b46e300fdd to your computer and use it in GitHub Desktop.
public static Color ToColor(this string color)
{
if (color.StartsWith("#", StringComparison.InvariantCulture))
{
color = color.Substring(1); // strip #
}
if (color.Length == 6)
{
color += "FF"; // add alpha if missing
}
var hex = Convert.ToUInt32(color, 16);
var r = ((hex & 0xff000000) >> 0x18) / 255f;
var g = ((hex & 0xff0000) >> 0x10) / 255f;
var b = ((hex & 0xff00) >> 8) / 255f;
var a = ((hex & 0xff)) / 255f;
return new Color(r, g, b, a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment