Ejemplo n.º 1
0
        // FIXME: Linear scan
        public static Color FindColorMatch(Color c)
        {
            uint argb = (uint)c.ToArgb();

            // 1-based
            const int first_real_color_index = (int)KnownColor.AliceBlue;
            const int last_real_color_index  = (int)KnownColor.YellowGreen;

            for (int i = first_real_color_index - 1; i < last_real_color_index; i++)
            {
                if (argb == KnownColors.ArgbValues[i])
                {
                    return(KnownColors.FromKnownColor((KnownColor)i));
                }
            }

            return(Color.Empty);
        }
Ejemplo n.º 2
0
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            lock (creatingCached)
            {
                if (cached != null)
                {
                    return(cached);
                }
                Array colors = Array.CreateInstance(typeof(Color), KnownColors.ArgbValues.Length - 1);
                for (int i = 1; i < KnownColors.ArgbValues.Length; i++)
                {
                    colors.SetValue(KnownColors.FromKnownColor((KnownColor)i), i - 1);
                }

                Array.Sort(colors, 0, colors.Length, new CompareColors());
                cached = new StandardValuesCollection(colors);
            }

            return(cached);
        }
Ejemplo n.º 3
0
 private Color CreateColor(KnownColor c)
 {
     return(KnownColors.FromKnownColor(c));
 }
Ejemplo n.º 4
0
        internal static Color StaticConvertFromString(ITypeDescriptorContext context, string s, CultureInfo culture)
        {
            if (culture == null)
            {
                culture = CultureInfo.InvariantCulture;
            }

            s = s.Trim();

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

            // Try to process both NamedColor and SystemColors from the KnownColor enumeration
            if (Char.IsLetter(s[0]))
            {
                KnownColor kc;
                try
                {
                    kc = (KnownColor)Enum.Parse(typeof(KnownColor), s, true);
                }
                catch (Exception e)
                {
                    // whatever happens MS throws an basic Exception
                    string msg = String.Format("Invalid color name '{0}'.", s);
                    throw new Exception(msg, new FormatException(msg, e));
                }
                return(KnownColors.FromKnownColor(kc));
            }

            String numSeparator = culture.TextInfo.ListSeparator;
            Color  result       = Color.Empty;

            try
            {
                result = new Color(s);
            }
            catch (Exception e)
            {
                // whatever happens MS throws an basic Exception
                string msg = String.Format("Invalid Int32 value '{0}'.", s);
                throw new Exception(msg, e);
            }


            if (result.IsEmpty)
            {
                Int32Converter converter  = new Int32Converter();
                String[]       components = s.Split(numSeparator.ToCharArray());

                // MS seems to convert the indivual component to int before
                // checking the number of components
                int[] numComponents = new int[components.Length];
                for (int i = 0; i < numComponents.Length; i++)
                {
                    numComponents[i] = (int)converter.ConvertFrom(context,
                                                                  culture, components[i]);
                }

                switch (components.Length)
                {
                case 1:
                    result = new Color(Convert.ToUInt32(numComponents[0]));
                    break;

                case 3:
                    result = new Color((byte)numComponents[0], (byte)numComponents[1],
                                       (byte)numComponents[2]);
                    break;

                case 4:
                    result = new Color((byte)numComponents[0], (byte)numComponents[1],
                                       (byte)numComponents[2], (byte)numComponents[3]);
                    break;

                default:
                    throw new ArgumentException(s + " is not a valid color value.");
                }
            }

            if (!result.IsEmpty)
            {
                // Look for a named or system color with those values
                Color known = KnownColors.FindColorMatch(result);
                if (!known.IsEmpty)
                {
                    return(known);
                }
            }

            return(result);
        }