コード例 #1
0
        public static Color ConvertFromString(string colorString, IFormatProvider formatProvider)
        {
            var normalized = colorString.Trim();

            if (normalized.StartsWith("#") && normalized.Length == 9)
            {
                if (normalized.Length == 9)
                {
                    var a = byte.Parse(colorString.Substring(1, 2), NumberStyles.HexNumber, formatProvider);
                    var r = byte.Parse(colorString.Substring(3, 2), NumberStyles.HexNumber, formatProvider);
                    var g = byte.Parse(colorString.Substring(5, 2), NumberStyles.HexNumber, formatProvider);
                    var b = byte.Parse(colorString.Substring(7, 2), NumberStyles.HexNumber, formatProvider);

                    return(Color.FromArgb(a, r, g, b));
                }

                if (normalized.Length == 7)
                {
                    var r = byte.Parse(colorString.Substring(1, 2), NumberStyles.HexNumber, formatProvider);
                    var g = byte.Parse(colorString.Substring(3, 2), NumberStyles.HexNumber, formatProvider);
                    var b = byte.Parse(colorString.Substring(5, 2), NumberStyles.HexNumber, formatProvider);

                    return(Color.FromArgb(0xFF, r, g, b));
                }
            }

            Color color;

            return(KnownColors.TryGetColor(normalized, out color) ? color : Colors.Transparent);
        }