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.ToLower ()) {
50 return SystemColors.Control;
52 return SystemColors.ActiveCaptionText;
53 case "threeddarkshadow":
54 return SystemColors.ControlDarkDark;
56 return SystemColors.Desktop;
58 return SystemColors.ControlText;
59 case "infobackground":
60 return SystemColors.Info;
61 // special case for Color.LightGray versus html's LightGrey (#340917)
63 return Color.LightGray;
65 TypeConverter converter = TypeDescriptor.GetConverter (typeof (Color));
66 return (Color) converter.ConvertFromString (htmlColor);
69 internal static Color FromBGR (int bgr)
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;
76 public static Color FromOle (int oleColor)
78 // OleColor format is BGR
79 return FromBGR (oleColor);
82 public static Color FromWin32 (int win32Color)
84 // Win32Color format is BGR
85 return FromBGR (win32Color);
88 public static string ToHtml (Color c)
93 if (c.IsSystemColor) {
94 KnownColor kc = c.ToKnownColor ();
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);
114 case KnownColor.ActiveCaptionText:
115 return "captiontext";
116 case KnownColor.Control:
118 case KnownColor.ControlDark:
119 return "buttonshadow";
120 case KnownColor.ControlDarkDark:
121 return "threeddarkshadow";
122 case KnownColor.ControlLight:
124 case KnownColor.ControlLightLight:
125 return "buttonhighlight";
126 case KnownColor.ControlText:
128 case KnownColor.Desktop:
130 case KnownColor.HotTrack:
132 case KnownColor.Info:
133 return "infobackground";
140 if (c.IsNamedColor) {
141 if (c == Color.LightGray)
147 return FormatHtml (c.R, c.G, c.B);
150 static char GetHexNumber (int b)
152 return (char) (b > 9 ? 55 + b : 48 + b);
155 static string FormatHtml (int r, int g, int b)
157 char [] htmlColor = new char [7];
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);
166 return new string (htmlColor);
169 public static int ToOle (Color c)
171 // OleColor format is BGR, same as Win32
172 return ((c.B << 16) | (c.G << 8) | c.R);
175 public static int ToWin32 (Color c)
177 // Win32Color format is BGR, Same as OleColor
178 return ((c.B << 16) | (c.G << 8) | c.R);