Example #1
0
        /// <summary>
        /// Parses a color string.
        /// </summary>
        /// <param name="s">The color string.</param>
        /// <param name="color">The parsed color</param>
        /// <returns>The status of the operation.</returns>
        public static bool TryParse(string s, out Color color)
        {
            if (s == null)
            {
                throw new ArgumentNullException(nameof(s));
            }

            if (s.Length == 0)
            {
                throw new FormatException();
            }

            if (s[0] == '#' && TryParseInternal(s.AsSpan(), out color))
            {
                return(true);
            }

            var knownColor = KnownColors.GetKnownColor(s);

            if (knownColor != KnownColor.None)
            {
                color = knownColor.ToColor();

                return(true);
            }

            color = default;

            return(false);
        }
Example #2
0
        /// <summary>
        /// Parses a color string.
        /// </summary>
        /// <param name="s">The color string.</param>
        /// <param name="color">The parsed color</param>
        /// <returns>The status of the operation.</returns>
        public static bool TryParse(string s, out Color color)
        {
            color = default;

            if (s is null)
            {
                return(false);
            }

            if (s.Length == 0)
            {
                return(false);
            }

            if (s[0] == '#' &&
                TryParseHexFormat(s.AsSpan(), out color))
            {
                return(true);
            }

            if (s.Length > 5 &&
                (s[0] == 'r' || s[0] == 'R') &&
                (s[1] == 'g' || s[1] == 'G') &&
                (s[2] == 'b' || s[2] == 'B') &&
                TryParseCssFormat(s, out color))
            {
                return(true);
            }

            if (s.Length > 5 &&
                (s[0] == 'h' || s[0] == 'H') &&
                (s[1] == 's' || s[1] == 'S') &&
                (s[2] == 'l' || s[2] == 'L') &&
                HslColor.TryParse(s, out HslColor hslColor))
            {
                color = hslColor.ToRgb();
                return(true);
            }

            if (s.Length > 5 &&
                (s[0] == 'h' || s[0] == 'H') &&
                (s[1] == 's' || s[1] == 'S') &&
                (s[2] == 'v' || s[2] == 'V') &&
                HsvColor.TryParse(s, out HsvColor hsvColor))
            {
                color = hsvColor.ToRgb();
                return(true);
            }

            var knownColor = KnownColors.GetKnownColor(s);

            if (knownColor != KnownColor.None)
            {
                color = knownColor.ToColor();
                return(true);
            }

            return(false);
        }
Example #3
0
        /// <summary>
        /// Parses a color string.
        /// </summary>
        /// <param name="s">The color string.</param>
        /// <param name="color">The parsed color</param>
        /// <returns>The status of the operation.</returns>
        public static bool TryParse(ReadOnlySpan <char> s, out Color color)
        {
            if (s.Length == 0)
            {
                color = default;
                return(false);
            }

            if (s[0] == '#' &&
                TryParseHexFormat(s, out color))
            {
                return(true);
            }

            // At this point all parsing uses strings
            var str = s.ToString();

            if (s.Length > 5 &&
                (s[0] == 'r' || s[0] == 'R') &&
                (s[1] == 'g' || s[1] == 'G') &&
                (s[2] == 'b' || s[2] == 'B') &&
                TryParseCssFormat(str, out color))
            {
                return(true);
            }

            if (s.Length > 5 &&
                (s[0] == 'h' || s[0] == 'H') &&
                (s[1] == 's' || s[1] == 'S') &&
                (s[2] == 'l' || s[2] == 'L') &&
                HslColor.TryParse(str, out HslColor hslColor))
            {
                color = hslColor.ToRgb();
                return(true);
            }

            if (s.Length > 5 &&
                (s[0] == 'h' || s[0] == 'H') &&
                (s[1] == 's' || s[1] == 'S') &&
                (s[2] == 'v' || s[2] == 'V') &&
                HsvColor.TryParse(str, out HsvColor hsvColor))
            {
                color = hsvColor.ToRgb();
                return(true);
            }

            var knownColor = KnownColors.GetKnownColor(str);

            if (knownColor != KnownColor.None)
            {
                color = knownColor.ToColor();
                return(true);
            }

            color = default;

            return(false);
        }
Example #4
0
        /// <summary>
        /// Parses a brush string.
        /// </summary>
        /// <param name="s">The brush string.</param>
        /// <returns>The <see cref="Color"/>.</returns>
        public static IBrush Parse(string s)
        {
            Contract.Requires <ArgumentNullException>(s != null);
            Contract.Requires <FormatException>(s.Length > 0);

            if (s[0] == '#')
            {
                return(new ImmutableSolidColorBrush(Color.Parse(s)));
            }

            var brush = KnownColors.GetKnownBrush(s);

            if (brush != null)
            {
                return(brush);
            }

            throw new FormatException($"Invalid brush string: '{s}'.");
        }
Example #5
0
        /// <summary>
        /// Parses a color string.
        /// </summary>
        /// <param name="s">The color string.</param>
        /// <param name="color">The parsed color</param>
        /// <returns>The status of the operation.</returns>
        public static bool TryParse(ReadOnlySpan <char> s, out Color color)
        {
            color = default;
            if (s == null)
            {
                return(false);
            }
            if (s.Length == 0)
            {
                return(false);
            }

            if (s[0] == '#')
            {
                var or = 0u;

                if (s.Length == 7)
                {
                    or = 0xff000000;
                }
                else if (s.Length != 9)
                {
                    return(false);
                }

                if (!uint.TryParse(s.Slice(1).ToString(), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var parsed))
                {
                    return(false);
                }
                color = FromUInt32(parsed | or);
                return(true);
            }

            var knownColor = KnownColors.GetKnownColor(s.ToString());

            if (knownColor != KnownColor.None)
            {
                color = knownColor.ToColor();
                return(true);
            }

            return(false);
        }
Example #6
0
        /// <summary>
        /// Parses a brush string.
        /// </summary>
        /// <param name="s">The brush string.</param>
        /// <returns>The <see cref="Color"/>.</returns>
        public static IBrush Parse(string s)
        {
            _ = s ?? throw new ArgumentNullException(nameof(s));

            if (s.Length > 0)
            {
                if (s[0] == '#')
                {
                    return(new ImmutableSolidColorBrush(Color.Parse(s)));
                }

                var brush = KnownColors.GetKnownBrush(s);
                if (brush != null)
                {
                    return(brush);
                }
            }

            throw new FormatException($"Invalid brush string: '{s}'.");
        }
Example #7
0
        /// <summary>
        /// Parses a color string.
        /// </summary>
        /// <param name="s">The color string.</param>
        /// <returns>The <see cref="Color"/>.</returns>
        public static Color Parse(string s)
        {
            if (s == null)
            {
                throw new ArgumentNullException(nameof(s));
            }
            if (s.Length == 0)
            {
                throw new FormatException();
            }

            if (s[0] == '#')
            {
                var or = 0u;

                if (s.Length == 7)
                {
                    or = 0xff000000;
                }
                else if (s.Length != 9)
                {
                    throw new FormatException($"Invalid color string: '{s}'.");
                }

                return(FromUInt32(uint.Parse(s.Substring(1), NumberStyles.HexNumber, CultureInfo.InvariantCulture) | or));
            }

            var knownColor = KnownColors.GetKnownColor(s);

            if (knownColor != KnownColor.None)
            {
                return(knownColor.ToColor());
            }

            throw new FormatException($"Invalid color string: '{s}'.");
        }
Example #8
0
        /// <summary>
        /// Returns the string representation of the color.
        /// </summary>
        /// <returns>
        /// The string representation of the color.
        /// </returns>
        public override string ToString()
        {
            uint rgb = ToUint32();

            return(KnownColors.GetKnownColorName(rgb) ?? $"#{rgb:x8}");
        }
Example #9
0
        /// <summary>
        /// Parses a color string.
        /// </summary>
        /// <param name="s">The color string.</param>
        /// <param name="color">The parsed color</param>
        /// <returns>The status of the operation.</returns>
        public static bool TryParse(ReadOnlySpan <char> s, out Color color)
        {
            if (s.Length == 0)
            {
                color = default;
                return(false);
            }

            if (s[0] == '#' &&
                TryParseHexFormat(s, out color))
            {
                return(true);
            }

            // At this point all parsing uses strings
            var str = s.ToString();

            // Note: The length checks are also an important optimization.
            // The shortest possible CSS format is "rbg(0,0,0)", Length = 10.

            if (s.Length >= 10 &&
                (s[0] == 'r' || s[0] == 'R') &&
                (s[1] == 'g' || s[1] == 'G') &&
                (s[2] == 'b' || s[2] == 'B') &&
                TryParseCssFormat(str, out color))
            {
                return(true);
            }

            if (s.Length >= 10 &&
                (s[0] == 'h' || s[0] == 'H') &&
                (s[1] == 's' || s[1] == 'S') &&
                (s[2] == 'l' || s[2] == 'L') &&
                HslColor.TryParse(str, out HslColor hslColor))
            {
                color = hslColor.ToRgb();
                return(true);
            }

            if (s.Length >= 10 &&
                (s[0] == 'h' || s[0] == 'H') &&
                (s[1] == 's' || s[1] == 'S') &&
                (s[2] == 'v' || s[2] == 'V') &&
                HsvColor.TryParse(str, out HsvColor hsvColor))
            {
                color = hsvColor.ToRgb();
                return(true);
            }

            var knownColor = KnownColors.GetKnownColor(str);

            if (knownColor != KnownColor.None)
            {
                color = knownColor.ToColor();
                return(true);
            }

            color = default;

            return(false);
        }
Example #10
0
        /// <summary>
        /// Parses a color string.
        /// </summary>
        /// <param name="s">The color string.</param>
        /// <param name="color">The parsed color</param>
        /// <returns>The status of the operation.</returns>
        public static bool TryParse(string s, out Color color)
        {
            color = default;

            if (s is null)
            {
                return(false);
            }

            if (s.Length == 0)
            {
                return(false);
            }

            if (s[0] == '#' &&
                TryParseHexFormat(s.AsSpan(), out color))
            {
                return(true);
            }

            // Note: The length checks are also an important optimization.
            // The shortest possible CSS format is "rbg(0,0,0)", Length = 10.

            if (s.Length >= 10 &&
                (s[0] == 'r' || s[0] == 'R') &&
                (s[1] == 'g' || s[1] == 'G') &&
                (s[2] == 'b' || s[2] == 'B') &&
                TryParseCssFormat(s, out color))
            {
                return(true);
            }

            if (s.Length >= 10 &&
                (s[0] == 'h' || s[0] == 'H') &&
                (s[1] == 's' || s[1] == 'S') &&
                (s[2] == 'l' || s[2] == 'L') &&
                HslColor.TryParse(s, out HslColor hslColor))
            {
                color = hslColor.ToRgb();
                return(true);
            }

            if (s.Length >= 10 &&
                (s[0] == 'h' || s[0] == 'H') &&
                (s[1] == 's' || s[1] == 'S') &&
                (s[2] == 'v' || s[2] == 'V') &&
                HsvColor.TryParse(s, out HsvColor hsvColor))
            {
                color = hsvColor.ToRgb();
                return(true);
            }

            var knownColor = KnownColors.GetKnownColor(s);

            if (knownColor != KnownColor.None)
            {
                color = knownColor.ToColor();
                return(true);
            }

            return(false);
        }