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 (string.IsNullOrEmpty (htmlColor)) return Color.Empty;
47 switch (htmlColor.ToLowerInvariant ()) {
50 return SystemColors.Control;
51 case "buttonhighlight":
52 case "threedlightshadow":
53 return SystemColors.ControlLightLight;
55 return SystemColors.ControlDark;
57 return SystemColors.ActiveCaptionText;
58 case "threeddarkshadow":
59 return SystemColors.ControlDarkDark;
60 case "threedhighlight":
61 return SystemColors.ControlLight;
63 return SystemColors.Desktop;
65 return SystemColors.ControlText;
66 case "infobackground":
67 return SystemColors.Info;
68 // special case for Color.LightGray versus html's LightGrey (#340917)
70 return Color.LightGray;
73 if (htmlColor[0] == '#' && htmlColor.Length == 4)
75 char r = htmlColor[1], g = htmlColor[2], b = htmlColor[3];
76 htmlColor = new string ( new char [] { '#', r, r, g, g, b, b } );
79 TypeConverter converter = TypeDescriptor.GetConverter (typeof (Color));
80 return (Color) converter.ConvertFromString (htmlColor);
83 internal static Color FromBGR (int bgr)
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;
90 public static Color FromOle (int oleColor)
92 // OleColor format is BGR
93 return FromBGR (oleColor);
96 public static Color FromWin32 (int win32Color)
98 // Win32Color format is BGR
99 return FromBGR (win32Color);
102 public static string ToHtml (Color c)
107 if (c.IsSystemColor) {
108 KnownColor kc = c.ToKnownColor ();
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);
128 case KnownColor.ActiveCaptionText:
129 return "captiontext";
130 case KnownColor.Control:
132 case KnownColor.ControlDark:
133 return "buttonshadow";
134 case KnownColor.ControlDarkDark:
135 return "threeddarkshadow";
136 case KnownColor.ControlLight:
138 case KnownColor.ControlLightLight:
139 return "buttonhighlight";
140 case KnownColor.ControlText:
142 case KnownColor.Desktop:
144 case KnownColor.HotTrack:
146 case KnownColor.Info:
147 return "infobackground";
154 if (c.IsNamedColor) {
155 if (c == Color.LightGray)
161 return FormatHtml (c.R, c.G, c.B);
164 static char GetHexNumber (int b)
166 return (char) (b > 9 ? 55 + b : 48 + b);
169 static string FormatHtml (int r, int g, int b)
171 char [] htmlColor = new char [7];
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);
180 return new string (htmlColor);
183 public static int ToOle (Color c)
185 // OleColor format is BGR, same as Win32
186 return ((c.B << 16) | (c.G << 8) | c.R);
189 public static int ToWin32 (Color c)
191 // Win32Color format is BGR, Same as OleColor
192 return ((c.B << 16) | (c.G << 8) | c.R);