예제 #1
0
        public static Color FromName(string name)
        {
            object namedColor = ColorConverter.GetNamedColor(name);

            if (namedColor != null)
            {
                return((Color)namedColor);
            }
            return(new Color(NotDefinedValue, StateNameValid, name, (KnownColor)0));
        }
예제 #2
0
        /// <summary>Converts the given object to the converter's native type.</summary>
        /// <returns>An <see cref="T:System.Object" /> representing the converted value.</returns>
        /// <param name="context">A <see cref="T:System.ComponentModel.TypeDescriptor" /> that provides a format context. You can use this object to get additional information about the environment from which this converter is being invoked. </param>
        /// <param name="culture">A <see cref="T:System.Globalization.CultureInfo" /> that specifies the culture to represent the color. </param>
        /// <param name="value">The object to convert. </param>
        /// <exception cref="T:System.ArgumentException">The conversion cannot be performed.</exception>
        /// <filterpriority>1</filterpriority>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
        /// </PermissionSet>
        public object ConvertFrom(CultureInfo culture, object value)
        {
            string text = value as string;

            if (text != null)
            {
                object obj   = null;
                string text2 = text.Trim();
                if (text2.Length == 0)
                {
                    obj = Color.Empty;
                }
                else
                {
                    obj = ColorConverter.GetNamedColor(text2);
                    if (obj == null)
                    {
                        if (culture == null)
                        {
                            culture = CultureInfo.CurrentCulture;
                        }
                        char c    = culture.TextInfo.ListSeparator[0];
                        bool flag = true;

                        if (text2.IndexOf(c) == -1)
                        {
                            if (text2.Length >= 2 && (text2[0] == '\'' || text2[0] == '"'))
                            {
                                char   arg_AC_0 = text2[0];
                                string expr_9F  = text2;
                                if (arg_AC_0 == expr_9F[expr_9F.Length - 1])
                                {
                                    obj  = Color.FromName(text2.Substring(1, text2.Length - 2));
                                    flag = false;
                                    goto IL_147;
                                }
                            }
                            if ((text2.Length == 7 && text2[0] == '#') || (text2.Length == 8 && (text2.StartsWith("0x") || text2.StartsWith("0X"))) || (text2.Length == 8 && (text2.StartsWith("&h") || text2.StartsWith("&H"))))
                            {
                                obj = Color.FromArgb(-16777216 | (int)int.Parse(text2, culture));
                            }
                        }
IL_147:
                        if (obj == null)
                        {
                            string[] array = text2.Split(new char[]
                            {
                                c
                            });
                            int[] array2 = new int[array.Length];
                            for (int i = 0; i < array2.Length; i++)
                            {
                                array2[i] = int.Parse(array[i], culture);
                            }
                            switch (array2.Length)
                            {
                            case 1:
                                obj = Color.FromArgb(array2[0]);
                                break;

                            case 3:
                                obj = Color.FromArgb(array2[0], array2[1], array2[2]);
                                break;

                            case 4:
                                obj = Color.FromArgb(array2[0], array2[1], array2[2], array2[3]);
                                break;
                            }
                            flag = true;
                        }
                        if (obj != null & flag)
                        {
                            int num = ((Color)obj).ToArgb();
                            foreach (Color color in ColorConverter.Colors.Values)
                            {
                                if (color.ToArgb() == num)
                                {
                                    obj = color;
                                    break;
                                }
                            }
                        }
                    }
                    if (obj == null)
                    {
                        throw new ArgumentException();
                    }
                }
                return(obj);
            }
            throw new ArgumentException();
        }