New test.
[mono.git] / mcs / class / System.Drawing / System.Drawing / ColorTranslator.cs
1 //
2 // System.Drawing.ColorTranslator.cs
3 //
4 // Authors:
5 //      Dennis Hayes (dennish@raytek.com)
6 //      Ravindra (rkumar@novell.com)
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // Copyright (C) 2001 Ximian, Inc.  http://www.ximian.com
10 // Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31  
32 using System.ComponentModel;
33
34 namespace System.Drawing {
35
36         public sealed class ColorTranslator {
37
38                 private ColorTranslator ()
39                 {
40                 }
41
42                 public static Color FromHtml (string htmlColor)
43                 {
44                         if ((htmlColor == null) || (htmlColor.Length == 0))
45                                 return Color.Empty;
46
47                         switch (htmlColor.ToLower ()) {
48                         case "buttonface":
49                                 return SystemColors.Control;
50                         case "captiontext":
51                                 return SystemColors.ActiveCaptionText;
52                         case "threeddarkshadow":
53                                 return SystemColors.ControlDarkDark;
54                         case "background":
55                                 return SystemColors.Desktop;
56                         case "buttontext":
57                                 return SystemColors.ControlText;
58                         case "infobackground":
59                                 return SystemColors.Info;
60                         }
61                         TypeConverter converter = TypeDescriptor.GetConverter (typeof (Color));
62                         return (Color) converter.ConvertFromString (htmlColor);
63                 }
64
65                 public static Color FromOle (int oleColor)
66                 {
67                         // OleColor format is BGR
68                         int R = oleColor & 0xFF;
69                         int G = (oleColor >> 8) & 0xFF;
70                         int B = (oleColor >> 16) & 0xFF;
71
72                         Color retcolor = Color.FromArgb (255, R, G, B);
73                         foreach (Color c in Color.NamedColors.Values) {
74                                 if (c == retcolor)
75                                         return c;
76                         }
77
78                         foreach (Color c in Color.SystemColors.Values) {
79                                 if (c == retcolor)
80                                         return c;
81                         }
82
83                         return retcolor;
84                 }
85
86                 public static Color FromWin32 (int win32Color)
87                 {
88                         // Win32Color format is BGR
89                         int R = win32Color & 0xFF;
90                         int G = (win32Color >> 8) & 0xFF;
91                         int B = (win32Color >> 16) & 0xFF;
92
93                         Color retcolor = Color.FromArgb (255, R, G, B);
94                         foreach (Color c in Color.NamedColors.Values) {
95                                 if (c == retcolor)
96                                         return c;
97                         }
98
99                         foreach (Color c in Color.SystemColors.Values) {
100                                 if (c == retcolor)
101                                         return c;
102                         }
103
104                         return retcolor;
105                 }
106
107                 public static string ToHtml (Color c)
108                 {
109                         KnownColor kc;
110                         if (c.IsEmpty)
111                                 return String.Empty;
112
113                         string result;
114                         if(c.IsSystemColor) {
115                                 kc = c.ToKnownColor();
116                                 switch (kc) {
117                                 case KnownColor.ActiveBorder:
118                                         return "activeborder";
119                                 case KnownColor.ActiveCaption:
120                                         return "activecaption";
121                                 case KnownColor.ActiveCaptionText:
122                                         return "captiontext";
123                                 case KnownColor.AppWorkspace:
124                                         return "appworkspace";
125                                 case KnownColor.Control:
126                                         return "buttonface";
127                                 case KnownColor.ControlDark:
128                                         return "buttonshadow";
129                                 case KnownColor.ControlDarkDark:
130                                         return "threeddarkshadow";
131                                 case KnownColor.ControlLight:
132                                         return "buttonface";
133                                 case KnownColor.ControlLightLight:
134                                         return "buttonhighlight";
135                                 case KnownColor.ControlText:
136                                         return "buttontext";
137                                 case KnownColor.Desktop:
138                                         return "background";
139                                 case KnownColor.GrayText:
140                                         return "graytext";
141                                 case KnownColor.Highlight:
142                                 case KnownColor.HotTrack:
143                                         return "highlight";
144                                 case KnownColor.HighlightText:
145                                         return "highlighttext";
146                                 case KnownColor.InactiveBorder:
147                                         return "inactiveborder";
148                                 case KnownColor.InactiveCaption:
149                                         return "inactivecaption";
150                                 case KnownColor.InactiveCaptionText:
151                                         return "inactivecaptiontext";
152                                 case KnownColor.Info:
153                                         return "infobackground";
154                                 case KnownColor.InfoText:
155                                         return "infotext";
156                                 case KnownColor.Menu:
157                                         return "menu";
158                                 case KnownColor.MenuText:
159                                         return "menutext";
160                                 case KnownColor.ScrollBar:
161                                         return "scrollbar";
162                                 case KnownColor.Window:
163                                         return "window";
164                                 case KnownColor.WindowFrame:
165                                         return "windowframe";
166                                 case KnownColor.WindowText:
167                                         return "windowtext";
168                                 default:
169                                         return String.Empty;
170                                 }
171                         }
172
173                         if (c.IsNamedColor) {
174                                 if (c == Color.LightGray) {
175                                         result =  "LightGrey";
176                                 }
177                                 else
178                                         result = c.Name;
179                         }
180                         else
181                                 result = String.Format ("#{0:X2}{1:X2}{2:X2}", c.R, c.G, c.B);
182
183                         return result;
184                 }
185
186                 public static int ToOle (Color c)
187                 {
188                         // OleColor format is BGR, same as Win32
189
190                         return  ((c.B << 16) | (c.G << 8) | c.R);
191                 }
192
193                 public static int ToWin32 (Color c)
194                 {
195                         // Win32Color format is BGR, Same as OleColor
196
197                         return  ((c.B << 16) | (c.G << 8) | c.R);
198                 }
199         }
200 }