2 // System.Drawing.ColorConverter
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
10 using System.Collections;
11 using System.ComponentModel;
12 using System.Globalization;
14 namespace System.Drawing {
16 public class ColorConverter : TypeConverter
18 static StandardValuesCollection cached;
19 static object creatingCached = new object ();
21 public ColorConverter ()
25 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
27 if (sourceType == typeof (string))
30 return base.CanConvertFrom(context, sourceType);
34 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
36 throw new NotImplementedException ();
39 public override object ConvertFrom (ITypeDescriptorContext context,
43 string s = value as string;
45 return base.ConvertFrom (context, culture, value);
47 object named = Color.NamedColors [s];
51 named = Color.SystemColors [s];
57 i = Int32.Parse (s.Substring (1), NumberStyles.HexNumber);
59 i = Int32.Parse (s, NumberStyles.Integer);
61 int A = (int) (i & 0xFF000000) >> 24;
65 Color result = Color.FromArgb (A, (i & 0x00FF0000) >> 16, (i & 0x00FF00) >> 8, (i & 0x0FF));
66 // Look for a named or system color with those values
67 foreach (Color c in Color.NamedColors.Values) {
68 if (c.A == result.A && c.R == result.R && c.G == result.G && c.B == result.B)
72 foreach (Color c in Color.SystemColors.Values) {
73 if (c.A == result.A && c.R == result.R && c.G == result.G && c.B == result.B)
81 public override object ConvertTo (ITypeDescriptorContext context,
86 throw new NotImplementedException ();
89 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
94 lock (creatingCached) {
98 ICollection named = (ICollection) Color.NamedColors;
99 ICollection system = (ICollection) Color.SystemColors;
100 Array colors = Array.CreateInstance (typeof (Color), named.Count + system.Count);
101 named.CopyTo (colors, 0);
102 system.CopyTo (colors, named.Count);
103 Array.Sort (colors, 0, colors.Length, new CompareColors ());
104 cached = new StandardValuesCollection (colors);
110 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
115 class CompareColors : IComparer
117 public int Compare (object x, object y)
119 return String.Compare (((Color) x).Name, ((Color) y).Name);