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