Example #1
0
        /// <summary>
        /// Text can be a hex code or a color name (see Color24.AllNamedColors).
        /// Hex codes can be preceded with a # or not.
        /// Color names are case-insensitive.
        /// Spaces in color names are ignored.
        /// </summary>
        public static bool TryParse(string text, out Color24 value)
        {
            text = text.Replace(" ", "");

            if (AllNamedColors.TryGetValue(text, out value))
            {
                return(true);
            }

            text  = text.TrimStart('#');
            value = default;

            if (text.Length != 6)
            {
                return(false);
            }

            if (Int32.TryParse(text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int hexNumber))
            {
                value = new Color24(hexNumber);
                return(true);
            }

            return(false);
        }
Example #2
0
        /// <summary>
        /// Text can be a hex code or a color name (see Color24.AllNamedColors).
        /// Hex codes can be preceded with a # or not.
        /// Color names are case-insensitive.
        /// Spaces in color names are ignored.
        /// </summary>
        public static bool TryParse(string text, out Color24 value)
        {
            text = text.Replace(" ", "");

            if (AllNamedColors.TryGetValue(text, out value))
            {
                return(true);
            }


            text = text.TrimStart('#');

            if (text.Length != 6)
            {
                // By default, TryParse will allow you to ignore leading zeroes, but we want to be sure that color codes are 6 digits
                value = default;
                return(false);
            }

            if (Int32.TryParse(text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int hexNumber))
            {
                value = new Color24(hexNumber);
                return(true);
            }

            value = default;
            return(false);
        }