Created
February 19, 2021 07:25
-
-
Save iletai/dbc4c6ab817ca9ab5785d4b46e300fdd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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