Final that also works on UWP :
public static Color GetColorFromHex(string hexString) { //add default transparency to ignore exception if ( !string.IsNullOrEmpty(hexString) && hexString.Length > 6 ) { if ( hexString.Length == 7 ) { hexString = "FF" + hexString; } hexString = hexString.Replace("#", string.Empty); byte a = (byte) ( Convert.ToUInt32(hexString.Substring(0, 2), 16) ); byte r = (byte) ( Convert.ToUInt32(hexString.Substring(2, 2), 16) ); byte g = (byte) ( Convert.ToUInt32(hexString.Substring(4, 2), 16) ); byte b = (byte) ( Convert.ToUInt32(hexString.Substring(6, 2), 16) ); Color color = Color.FromArgb(a, r, g, b); return color; } //return black if hex is null or invalid return Color.FromArgb(255, 0, 0, 0); }
source share