[System.Drawing] Add support for a null culture argument to the ConvertFrom () method...
[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-2007 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 using System.Globalization;
34
35 namespace System.Drawing {
36
37         public sealed class ColorTranslator {
38
39                 private ColorTranslator ()
40                 {
41                 }
42
43                 public static Color FromHtml (string htmlColor)
44                 {
45                         if ((htmlColor == null) || (htmlColor.Length == 0))
46                                 return Color.Empty;
47
48                         switch (htmlColor.ToLowerInvariant ()) {
49                         case "buttonface":
50                         case "threedface":              
51                                 return SystemColors.Control;
52                         case "buttonhighlight":
53                         case "threedlightshadow":
54                                 return SystemColors.ControlLightLight;
55                         case "buttonshadow":
56                                 return SystemColors.ControlDark;
57                         case "captiontext":
58                                 return SystemColors.ActiveCaptionText;
59                         case "threeddarkshadow":
60                                 return SystemColors.ControlDarkDark;
61                         case "threedhighlight":
62                                 return SystemColors.ControlLight;
63                         case "background":
64                                 return SystemColors.Desktop;
65                         case "buttontext":
66                                 return SystemColors.ControlText;
67                         case "infobackground":
68                                 return SystemColors.Info;
69                         // special case for Color.LightGray versus html's LightGrey (#340917)
70                         case "lightgrey":
71                                 return Color.LightGray;
72                         }
73                         
74                         TypeConverter converter = TypeDescriptor.GetConverter (typeof (Color));
75                         return (Color) converter.ConvertFromString (htmlColor);
76                 }
77
78                 internal static Color FromBGR (int bgr)
79                 {
80                         Color result = Color.FromArgb (0xFF, (bgr & 0xFF), ((bgr >> 8) & 0xFF), ((bgr >> 16) & 0xFF));
81                         Color known = KnownColors.FindColorMatch (result);
82                         return (known.IsEmpty) ? result : known;
83                 }
84
85                 public static Color FromOle (int oleColor)
86                 {
87                         // OleColor format is BGR
88                         return FromBGR (oleColor);
89                 }
90
91                 public static Color FromWin32 (int win32Color)
92                 {
93                         // Win32Color format is BGR
94                         return FromBGR (win32Color);
95                 }
96
97                 public static string ToHtml (Color c)
98                 {
99                         if (c.IsEmpty)
100                                 return String.Empty;
101
102                         if (c.IsSystemColor) {
103                                 KnownColor kc = c.ToKnownColor ();
104                                 switch (kc) {
105                                 case KnownColor.ActiveBorder:
106                                 case KnownColor.ActiveCaption:
107                                 case KnownColor.AppWorkspace:
108                                 case KnownColor.GrayText:
109                                 case KnownColor.Highlight:
110                                 case KnownColor.HighlightText:
111                                 case KnownColor.InactiveBorder:
112                                 case KnownColor.InactiveCaption:
113                                 case KnownColor.InactiveCaptionText:
114                                 case KnownColor.InfoText:
115                                 case KnownColor.Menu:
116                                 case KnownColor.MenuText:
117                                 case KnownColor.ScrollBar:
118                                 case KnownColor.Window:
119                                 case KnownColor.WindowFrame:
120                                 case KnownColor.WindowText:
121                                         return KnownColors.GetName (kc).ToLower (CultureInfo.InvariantCulture);
122
123                                 case KnownColor.ActiveCaptionText:
124                                         return "captiontext";
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.HotTrack:
140                                         return "highlight";
141                                 case KnownColor.Info:
142                                         return "infobackground";
143
144                                 default:
145                                         return String.Empty;
146                                 }
147                         }
148
149                         if (c.IsNamedColor) {
150                                 if (c == Color.LightGray)
151                                         return "LightGrey";
152                                 else
153                                         return c.Name;
154                         }
155
156                         return FormatHtml (c.R, c.G, c.B);
157                 }
158
159                 static char GetHexNumber (int b)
160                 {
161                         return (char) (b > 9 ? 55 + b : 48 + b);
162                 }
163
164                 static string FormatHtml (int r, int g, int b)
165                 {
166                         char [] htmlColor = new char [7];
167                         htmlColor [0] = '#';
168                         htmlColor [1] = GetHexNumber ((r >> 4) & 15);
169                         htmlColor [2] = GetHexNumber (r & 15);
170                         htmlColor [3] = GetHexNumber ((g >> 4) & 15);
171                         htmlColor [4] = GetHexNumber (g & 15);
172                         htmlColor [5] = GetHexNumber ((b >> 4) & 15);
173                         htmlColor [6] = GetHexNumber (b & 15);
174
175                         return new string (htmlColor);
176                 }
177
178                 public static int ToOle (Color c)
179                 {
180                         // OleColor format is BGR, same as Win32
181                         return  ((c.B << 16) | (c.G << 8) | c.R);
182                 }
183
184                 public static int ToWin32 (Color c)
185                 {
186                         // Win32Color format is BGR, Same as OleColor
187                         return  ((c.B << 16) | (c.G << 8) | c.R);
188                 }
189         }
190 }