Exemplo n.º 1
0
        public static Color FromName(string name)
        {
            if (ColorTable.TryGetNamedColor(name, out Color result))
            {
                return(result);
            }

            return(new Color(0L, 8, name, (KnownColor)0));
        }
Exemplo n.º 2
0
        public static Color FromName(string name)
        {
            // try to get a known color first
            if (ColorTable.TryGetNamedColor(name, out Color color))
            {
                return(color);
            }

            // otherwise treat it as a named color
            return(new Color(NotDefinedValue, StateNameValid, name, (KnownColor)0));
        }
Exemplo n.º 3
0
        public static Color FromName(string name)
        {
            // try to get a known color first
            Color color;

            if (ColorTable.TryGetNamedColor(name, out color))
            {
                return(color);
            }
            // otherwise treat it as a named color
            return((Color)s_ctorAllValues.Invoke(new object[] { NotDefinedValue, StateNameValid, name, 0 }));
        }
Exemplo n.º 4
0
        public static Color ConvertFromString(string strValue, CultureInfo culture)
        {
            Debug.Assert(culture != null);

            string text = strValue.Trim();

            if (text.Length == 0)
            {
                return(Color.Empty);
            }

            {
                Color c;
                // First, check to see if this is a standard name.
                //
                if (ColorTable.TryGetNamedColor(text, out c))
                {
                    return(c);
                }
            }

            char sep = culture.TextInfo.ListSeparator[0];

            // If the value is a 6 digit hex number only, then
            // we want to treat the Alpha as 255, not 0
            //
            if (!text.Contains(sep))
            {
                // text can be '' (empty quoted string)
                if (text.Length >= 2 && (text[0] == '\'' || text[0] == '"') && text[0] == text[text.Length - 1])
                {
                    // In quotes means a named value
                    string colorName = text.Substring(1, text.Length - 2);
                    return(Color.FromName(colorName));
                }
                else if ((text.Length == 7 && text[0] == '#') ||
                         (text.Length == 8 && (text.StartsWith("0x") || text.StartsWith("0X"))) ||
                         (text.Length == 8 && (text.StartsWith("&h") || text.StartsWith("&H"))))
                {
                    // Note: int.Parse will raise exception if value cannot be converted.
                    return(PossibleKnownColor(Color.FromArgb(unchecked ((int)(0xFF000000 | (uint)IntFromString(text, culture))))));
                }
            }

            // Nope. Parse the RGBA from the text.
            //
            string[] tokens = text.Split(sep);
            int[]    values = new int[tokens.Length];
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = unchecked (IntFromString(tokens[i], culture));
            }

            // We should now have a number of parsed integer values.
            // We support 1, 3, or 4 arguments:
            //
            // 1 -- full ARGB encoded
            // 3 -- RGB
            // 4 -- ARGB
            //
            return(values.Length switch
            {
                1 => PossibleKnownColor(Color.FromArgb(values[0])),
                3 => PossibleKnownColor(Color.FromArgb(values[0], values[1], values[2])),
                4 => PossibleKnownColor(Color.FromArgb(values[0], values[1], values[2], values[3])),
                _ => throw new ArgumentException(SR.Format(SR.InvalidColor, text)),
            });
Exemplo n.º 5
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string strValue = value as string;

            if (strValue != null)
            {
                string text = strValue.Trim();

                if (text.Length == 0)
                {
                    return(Color.Empty);
                }

                {
                    Color c;
                    // First, check to see if this is a standard name.
                    //
                    if (ColorTable.TryGetNamedColor(text, out c))
                    {
                        return(c);
                    }
                }

                if (culture == null)
                {
                    culture = CultureInfo.CurrentCulture;
                }

                char sep = culture.TextInfo.ListSeparator[0];

                TypeConverter intConverter = TypeDescriptor.GetConverter(typeof(int));

                // If the value is a 6 digit hex number only, then
                // we want to treat the Alpha as 255, not 0
                //
                if (text.IndexOf(sep) == -1)
                {
                    // text can be '' (empty quoted string)
                    if (text.Length >= 2 && (text[0] == '\'' || text[0] == '"') && text[0] == text[text.Length - 1])
                    {
                        // In quotes means a named value
                        string colorName = text.Substring(1, text.Length - 2);
                        return(Color.FromName(colorName));
                    }
                    else if ((text.Length == 7 && text[0] == '#') ||
                             (text.Length == 8 && (text.StartsWith("0x") || text.StartsWith("0X"))) ||
                             (text.Length == 8 && (text.StartsWith("&h") || text.StartsWith("&H"))))
                    {
                        // Note: ConvertFromString will raise exception if value cannot be converted.
                        return(PossibleKnownColor(Color.FromArgb(unchecked ((int)(0xFF000000 | (uint)(int)intConverter.ConvertFromString(context, culture, text))))));
                    }
                }

                // Nope.  Parse the RGBA from the text.
                //
                string[] tokens = text.Split(sep);
                int[]    values = new int[tokens.Length];
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = unchecked ((int)intConverter.ConvertFromString(context, culture, tokens[i]));
                }

                // We should now have a number of parsed integer values.
                // We support 1, 3, or 4 arguments:
                //
                // 1 -- full ARGB encoded
                // 3 -- RGB
                // 4 -- ARGB
                //
                switch (values.Length)
                {
                case 1:
                    return(PossibleKnownColor(Color.FromArgb(values[0])));

                case 3:
                    return(PossibleKnownColor(Color.FromArgb(values[0], values[1], values[2])));

                case 4:
                    return(PossibleKnownColor(Color.FromArgb(values[0], values[1], values[2], values[3])));
                }

                throw new ArgumentException(SR.Format(SR.InvalidColor, text));
            }
            return(base.ConvertFrom(context, culture, value));
        }