2 // System.Drawing.ColorTranslator.cs
4 // Copyright (C) 2001 Ximian, Inc. http://www.ximian.com
5 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
8 // Dennis Hayes (dennish@raytek.com)
9 // Ravindra (rkumar@novell.com)
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System.ComponentModel;
39 namespace System.Drawing
41 public sealed class ColorTranslator
43 private ColorTranslator () { }
45 public static Color FromHtml (string HtmlFromColor)
47 switch (HtmlFromColor.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 (HtmlFromColor);
65 public static Color FromOle (int OleFromColor)
67 // OleColor format is BGR
68 int R = OleFromColor & 0xFF;
69 int G = (OleFromColor >> 8) & 0xFF;
70 int B = (OleFromColor >> 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 Win32FromColor)
88 // Win32Color format is BGR
89 int R = Win32FromColor & 0xFF;
90 int G = (Win32FromColor >> 8) & 0xFF;
91 int B = (Win32FromColor >> 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 color)
188 // OleColor format is BGR, same as Win32
190 return ((color.B << 16) | (color.G << 8) | color.R);
193 public static int ToWin32 (Color color)
195 // Win32Color format is BGR, Same as OleColor
197 return ((color.B << 16) | (color.G << 8) | color.R);