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 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;
34 namespace System.Drawing {
36 public sealed class ColorTranslator {
38 private ColorTranslator ()
42 public static Color FromHtml (string htmlColor)
44 if ((htmlColor == null) || (htmlColor.Length == 0))
47 switch (htmlColor.ToLower ()) {
49 return SystemColors.Control;
51 return SystemColors.ActiveCaptionText;
52 case "threeddarkshadow":
53 return SystemColors.ControlDarkDark;
55 return SystemColors.Desktop;
57 return SystemColors.ControlText;
58 case "infobackground":
59 return SystemColors.Info;
61 TypeConverter converter = TypeDescriptor.GetConverter (typeof (Color));
62 return (Color) converter.ConvertFromString (htmlColor);
65 public static Color FromOle (int oleColor)
67 // OleColor format is BGR
68 int R = oleColor & 0xFF;
69 int G = (oleColor >> 8) & 0xFF;
70 int B = (oleColor >> 16) & 0xFF;
72 Color retcolor = Color.FromArgb (255, R, G, B);
73 foreach (Color c in Color.NamedColors.Values) {
78 foreach (Color c in Color.SystemColors.Values) {
86 public static Color FromWin32 (int win32Color)
88 // Win32Color format is BGR
89 int R = win32Color & 0xFF;
90 int G = (win32Color >> 8) & 0xFF;
91 int B = (win32Color >> 16) & 0xFF;
93 Color retcolor = Color.FromArgb (255, R, G, B);
94 foreach (Color c in Color.NamedColors.Values) {
99 foreach (Color c in Color.SystemColors.Values) {
107 public static string ToHtml (Color c)
114 if(c.IsSystemColor) {
115 kc = c.ToKnownColor();
117 case KnownColor.ActiveBorder:
118 return "activeborder";
119 case KnownColor.ActiveCaption:
120 return "activecaption";
121 case KnownColor.ActiveCaptionText:
122 return "captiontext";
123 case KnownColor.AppWorkspace:
124 return "appworkspace";
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.GrayText:
141 case KnownColor.Highlight:
142 case KnownColor.HotTrack:
144 case KnownColor.HighlightText:
145 return "highlighttext";
146 case KnownColor.InactiveBorder:
147 return "inactiveborder";
148 case KnownColor.InactiveCaption:
149 return "inactivecaption";
150 case KnownColor.InactiveCaptionText:
151 return "inactivecaptiontext";
152 case KnownColor.Info:
153 return "infobackground";
154 case KnownColor.InfoText:
156 case KnownColor.Menu:
158 case KnownColor.MenuText:
160 case KnownColor.ScrollBar:
162 case KnownColor.Window:
164 case KnownColor.WindowFrame:
165 return "windowframe";
166 case KnownColor.WindowText:
173 if (c.IsNamedColor) {
174 if (c == Color.LightGray) {
175 result = "LightGrey";
181 result = String.Format ("#{0:X2}{1:X2}{2:X2}", c.R, c.G, c.B);
186 public static int ToOle (Color c)
188 // OleColor format is BGR, same as Win32
190 return ((c.B << 16) | (c.G << 8) | c.R);
193 public static int ToWin32 (Color c)
195 // Win32Color format is BGR, Same as OleColor
197 return ((c.B << 16) | (c.G << 8) | c.R);