2 // System.Drawing.ColorTranslator.cs
5 // Dennis Hayes (dennish@raytek.com)
6 // Ravindra (rkumar@novell.com)
7 // Sebastien Pouliot <sebastien@ximian.com>
9 // Copyright (C) 2001 Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2004,2006-2007 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 using System.ComponentModel;
33 using System.Globalization;
35 namespace System.Drawing {
37 public sealed class ColorTranslator {
39 private ColorTranslator ()
43 public static Color FromHtml (string htmlColor)
45 if ((htmlColor == null) || (htmlColor.Length == 0))
48 switch (htmlColor.ToLowerInvariant ()) {
51 return SystemColors.Control;
52 case "buttonhighlight":
53 case "threedlightshadow":
54 return SystemColors.ControlLightLight;
56 return SystemColors.ControlDark;
58 return SystemColors.ActiveCaptionText;
59 case "threeddarkshadow":
60 return SystemColors.ControlDarkDark;
61 case "threedhighlight":
62 return SystemColors.ControlLight;
64 return SystemColors.Desktop;
66 return SystemColors.ControlText;
67 case "infobackground":
68 return SystemColors.Info;
69 // special case for Color.LightGray versus html's LightGrey (#340917)
71 return Color.LightGray;
74 TypeConverter converter = TypeDescriptor.GetConverter (typeof (Color));
75 return (Color) converter.ConvertFromString (htmlColor);
78 internal static Color FromBGR (int bgr)
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;
85 public static Color FromOle (int oleColor)
87 // OleColor format is BGR
88 return FromBGR (oleColor);
91 public static Color FromWin32 (int win32Color)
93 // Win32Color format is BGR
94 return FromBGR (win32Color);
97 public static string ToHtml (Color c)
102 if (c.IsSystemColor) {
103 KnownColor kc = c.ToKnownColor ();
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);
123 case KnownColor.ActiveCaptionText:
124 return "captiontext";
125 case KnownColor.Control:
127 case KnownColor.ControlDark:
128 return "buttonshadow";
129 case KnownColor.ControlDarkDark:
130 return "threeddarkshadow";
131 case KnownColor.ControlLight:
133 case KnownColor.ControlLightLight:
134 return "buttonhighlight";
135 case KnownColor.ControlText:
137 case KnownColor.Desktop:
139 case KnownColor.HotTrack:
141 case KnownColor.Info:
142 return "infobackground";
149 if (c.IsNamedColor) {
150 if (c == Color.LightGray)
156 return FormatHtml (c.R, c.G, c.B);
159 static char GetHexNumber (int b)
161 return (char) (b > 9 ? 55 + b : 48 + b);
164 static string FormatHtml (int r, int g, int b)
166 char [] htmlColor = new char [7];
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);
175 return new string (htmlColor);
178 public static int ToOle (Color c)
180 // OleColor format is BGR, same as Win32
181 return ((c.B << 16) | (c.G << 8) | c.R);
184 public static int ToWin32 (Color c)
186 // Win32Color format is BGR, Same as OleColor
187 return ((c.B << 16) | (c.G << 8) | c.R);