svn path=/branches/mono-1-1-9/mcs/; revision=50438
[mono.git] / mcs / class / System.Drawing / System.Drawing / ColorTranslator.cs
1 //
2 // System.Drawing.ColorTranslator.cs
3 //
4 // Copyright (C) 2001 Ximian, Inc.  http://www.ximian.com
5 // Copyright (C) 2004 Novell, Inc.  http://www.novell.com
6 //
7 // Authors:
8 //      Dennis Hayes (dennish@raytek.com)
9 //      Ravindra (rkumar@novell.com)
10 //
11 //
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35  
36 using System;
37 using System.ComponentModel;
38
39 namespace System.Drawing
40 {
41         public sealed class ColorTranslator
42         {
43                 private ColorTranslator () { }
44
45                 public static Color FromHtml (string HtmlFromColor)
46                 {
47                         switch (HtmlFromColor.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 (HtmlFromColor);
63                 }
64
65                 public static Color FromOle (int OleFromColor)
66                 {
67                         // OleColor format is BGR
68                         int R = OleFromColor & 0xFF;
69                         int G = (OleFromColor >> 8) & 0xFF;
70                         int B = (OleFromColor >> 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 Win32FromColor)
87                 {
88                         // Win32Color format is BGR
89                         int R = Win32FromColor & 0xFF;
90                         int G = (Win32FromColor >> 8) & 0xFF;
91                         int B = (Win32FromColor >> 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 "";
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 color)
187                 {
188                         // OleColor format is BGR, same as Win32
189
190                         return  ((color.B << 16) | (color.G << 8) | color.R);
191                 }
192
193                 public static int ToWin32 (Color color)
194                 {
195                         // Win32Color format is BGR, Same as OleColor
196
197                         return  ((color.B << 16) | (color.G << 8) | color.R);
198                 }
199         }
200 }