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