19 using System.Globalization;
20 using Newtonsoft.Json;
35 protected override string Convert(Color value)
37 return value.IsEmpty ?
string.Empty : $
"#{value.R.ToStringInvariant("X2
")}{value.G.ToStringInvariant("X2
")}{value.B.ToStringInvariant("X2
")}";
45 protected override Color
Convert(
string value)
47 if (
string.IsNullOrWhiteSpace(value))
51 if (value.Length != 7)
53 var message = $
"Unable to convert '{value}' to a Color. Requires string length of 7 including the leading hashtag.";
54 throw new FormatException(message);
57 var red = HexToInt(value.Substring(1, 2));
58 var green = HexToInt(value.Substring(3, 2));
59 var blue = HexToInt(value.Substring(5, 2));
60 return Color.FromArgb(red, green, blue);
68 private int HexToInt(
string hexValue)
70 if (hexValue.Length != 2)
72 var message = $
"Unable to convert '{hexValue}' to an Integer. Requires string length of 2.";
73 throw new FormatException(message);
77 if (!
int.TryParse(hexValue, NumberStyles.HexNumber,
null, out result))
79 throw new FormatException($
"Invalid hex number: {hexValue}");