public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string strValue)
            {
                string text = strValue.Trim();
                if (text.Length == 0)
                {
                    return(null);
                }

                // Parse 2 integer values.
                if (culture == null)
                {
                    culture = CultureInfo.CurrentCulture;
                }

                char          sep          = culture.TextInfo.ListSeparator[0];
                string[]      tokens       = text.Split(sep);
                int[]         values       = new int[tokens.Length];
                TypeConverter intConverter = TypeDescriptor.GetConverter(typeof(int));
                for (int i = 0; i < values.Length; i++)
                {
                    // Note: ConvertFromString will raise exception if value cannot be converted.
                    values[i] = (int)intConverter.ConvertFromString(context, culture, tokens[i]);
                }

                if (values.Length == 2)
                {
                    return(new Point(values[0], values[1]));
                }
                else
                {
                    throw new ArgumentException(SR.Format(SR.TextParseFailedFormat, text, "x, y"));
                }
            }

            return(base.ConvertFrom(context, culture, value));
        }
Beispiel #2
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string strValue = value as string;

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

                if (text.Length == 0)
                {
                    obj = Color.Empty;
                }
                else
                {
                    // First, check to see if this is a standard name.
                    //
                    obj = GetNamedColor(text);

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

                        char sep = culture.TextInfo.ListSeparator[0];
                        bool tryMappingToKnownColor = true;

                        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);
                                obj = Color.FromName(colorName);
                                tryMappingToKnownColor = false;
                            }
                            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.
                                obj = Color.FromArgb(unchecked ((int)(0xFF000000 | (uint)(int)intConverter.ConvertFromString(context, culture, text))));
                            }
                        }

                        // Nope.  Parse the RGBA from the text.
                        //
                        if (obj == null)
                        {
                            string[] tokens = text.Split(new char[] { 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:
                                obj = Color.FromArgb(values[0]);
                                break;

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

                            case 4:
                                obj = Color.FromArgb(values[0], values[1], values[2], values[3]);
                                break;
                            }
                            tryMappingToKnownColor = true;
                        }

                        if ((obj != null) && tryMappingToKnownColor)
                        {
                            // Now check to see if this color matches one of our known colors.
                            // If it does, then substitute it.  We can only do this for "Colors"
                            // because system colors morph with user settings.
                            //
                            int targetARGB = ((Color)obj).ToArgb();

                            foreach (Color c in Colors.Values)
                            {
                                if (c.ToArgb() == targetARGB)
                                {
                                    obj = c;
                                    break;
                                }
                            }
                        }
                    }

                    if (obj == null)
                    {
                        //throw new ArgumentException(SR.GetString(SR.InvalidColor, text));
                    }
                }
                return(obj);
            }
            return(base.ConvertFrom(context, culture, value));
        }
Beispiel #3
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string str = value as string;

            if (str == null)
            {
                return(base.ConvertFrom(context, culture, value));
            }
            object namedColor = null;
            string name       = str.Trim();

            if (name.Length == 0)
            {
                return(Color.Empty);
            }
            namedColor = GetNamedColor(name);
            if (namedColor == null)
            {
                if (culture == null)
                {
                    culture = CultureInfo.CurrentCulture;
                }
                char          ch        = culture.TextInfo.ListSeparator[0];
                bool          flag      = true;
                TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
                if (name.IndexOf(ch) == -1)
                {
                    if (((name.Length >= 2) && ((name[0] == '\'') || (name[0] == '"'))) && (name[0] == name[name.Length - 1]))
                    {
                        namedColor = Color.FromName(name.Substring(1, name.Length - 2));
                        flag       = false;
                    }
                    else if ((((name.Length == 7) && (name[0] == '#')) || ((name.Length == 8) && (name.StartsWith("0x") || name.StartsWith("0X")))) || ((name.Length == 8) && (name.StartsWith("&h") || name.StartsWith("&H"))))
                    {
                        namedColor = Color.FromArgb(-16777216 | ((int)converter.ConvertFromString(context, culture, name)));
                    }
                }
                if (namedColor == null)
                {
                    string[] strArray = name.Split(new char[] { ch });
                    int[]    numArray = new int[strArray.Length];
                    for (int i = 0; i < numArray.Length; i++)
                    {
                        numArray[i] = (int)converter.ConvertFromString(context, culture, strArray[i]);
                    }
                    switch (numArray.Length)
                    {
                    case 1:
                        namedColor = Color.FromArgb(numArray[0]);
                        break;

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

                    case 4:
                        namedColor = Color.FromArgb(numArray[0], numArray[1], numArray[2], numArray[3]);
                        break;
                    }
                    flag = true;
                }
                if ((namedColor != null) && flag)
                {
                    int num2 = ((Color)namedColor).ToArgb();
                    foreach (Color color in Colors.Values)
                    {
                        if (color.ToArgb() == num2)
                        {
                            namedColor = color;
                            break;
                        }
                    }
                }
            }
            if (namedColor == null)
            {
                throw new ArgumentException(System.Drawing.SR.GetString("InvalidColor", new object[] { name }));
            }
            return(namedColor);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, 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 = GetNamedColor(text2);
                    if (obj == null)
                    {
                        if (culture == null)
                        {
                            culture = CultureInfo.CurrentCulture;
                        }

                        char          c         = culture.TextInfo.ListSeparator[0];
                        bool          flag      = true;
                        TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
                        if (text2.IndexOf(c) == -1)
                        {
                            if (text2.Length >= 2 && (text2[0] == '\'' || text2[0] == '"') &&
                                text2[0] == text2[text2.Length - 1])
                            {
                                obj  = Color.FromName(text2.Substring(1, text2.Length - 2));
                                flag = false;
                            }
                            else 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)converter.ConvertFromString(context, culture, text2));
                            }
                        }

                        if (obj == null)
                        {
                            string[] array  = text2.Split(c);
                            int[]    array2 = new int[array.Length];
                            for (int i = 0; i < array2.Length; i++)
                            {
                                array2[i] = (int)converter.ConvertFromString(context, culture, array[i]);
                            }

                            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 value2 in Colors.Values)
                            {
                                if (value2.ToArgb() == num)
                                {
                                    obj = value2;
                                    break;
                                }
                            }
                        }
                    }

                    if (obj == null)
                    {
                        throw new ArgumentException(SR.Format("Color '{0}' is not valid.", text2));
                    }
                }

                return(obj);
            }

            return(base.ConvertFrom(context, culture, value));
        }
Beispiel #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));
        }