2003-04-26 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Drawing / System.Drawing / ColorConverter.cs
1 //
2 // System.Drawing.ColorConverter
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 //
9 using System;
10 using System.Collections;
11 using System.ComponentModel;
12 using System.Globalization;
13
14 namespace System.Drawing {
15
16 public class ColorConverter : TypeConverter
17 {
18         static StandardValuesCollection cached;
19         static object creatingCached = new object ();
20
21         public ColorConverter ()
22         {
23         }
24
25         public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
26         {
27                 if (sourceType == typeof (string))
28                         return true;
29
30                 return base.CanConvertFrom(context, sourceType);
31         }
32
33         [MonoTODO]
34         public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
35         {
36                 throw new NotImplementedException ();
37         }
38
39         public override object ConvertFrom (ITypeDescriptorContext context,
40                                             CultureInfo culture,
41                                             object value)
42         {
43                 string s = value as string;
44                 if (s == null)
45                         return base.ConvertFrom (context, culture, value);
46
47                 object named = Color.NamedColors [s];
48                 if (named != null)
49                         return (Color) named;
50
51                 named = Color.SystemColors [s];
52                 if (named != null)
53                         return (Color) named;
54                         
55                 int i;
56                 if (s [0] == '#')
57                         i = Int32.Parse (s.Substring (1), NumberStyles.HexNumber);
58                 else
59                         i = Int32.Parse (s, NumberStyles.Integer);
60
61                 int A = (int) (i & 0xFF000000) >> 24;
62                 if (A == 0)
63                         A = 255;
64
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)
69                                 return c;
70                 }
71
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)
74                                 return c;
75                 }
76
77                 return result;
78         }
79
80         [MonoTODO]
81         public override object ConvertTo (ITypeDescriptorContext context,
82                                           CultureInfo culture,
83                                           object value,
84                                           Type destinationType)
85         {
86                 throw new NotImplementedException ();
87         }
88
89         public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
90         {
91                 if (cached != null)
92                         return cached;
93
94                 lock (creatingCached) {
95                         if (cached != null)
96                                 return cached;
97                         
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);
105                 }
106
107                 return cached;
108         }
109
110         public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
111         {
112                 return true;
113         }
114
115         class CompareColors : IComparer
116         {
117                 public int Compare (object x, object y)
118                 {
119                         return String.Compare (((Color) x).Name, ((Color) y).Name);
120                 }
121         }
122 }
123 }
124