-//\r
-// System.Drawing.ColorTranslator.cs\r
-//\r
-// (C) 2001 Ximian, Inc. http://www.ximian.com\r
-//\r
-// Dennis Hayes (dennish@raytek.com)\r
-// Inital Implimentation 3/25/2002\r
-// All conversions based on best guess, will improve over time\r
-// \r
-using System;\r
-namespace System.Drawing {\r
- public sealed class ColorTranslator{
+//
+// System.Drawing.ColorTranslator.cs
+//
+// Authors:
+// Dennis Hayes (dennish@raytek.com)
+// Ravindra (rkumar@novell.com)
+// Sebastien Pouliot <sebastien@ximian.com>
+//
+// Copyright (C) 2001 Ximian, Inc. http://www.ximian.com
+// Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System.ComponentModel;
+
+namespace System.Drawing {
+
+ public sealed class ColorTranslator {
private ColorTranslator ()
{
}
-\r
- // From converisons\r
- /// <summary>\r
- /// \r
- /// </summary>\r
- /// <param name="HtmlFromColor"></param>\r
- /// <returns></returns>\r
- public static Color FromHtml(string HtmlFromColor){\r
- // If first char is "#"\r
- //convert "#RRGGBB" to int and use Color.FromARGB(int) to create color\r
- // else //it is a color name\r
- //If there is a single digit at the end of the name, remove it.\r
- // Call Color.FromKnownColor(HtmlFromColor) \r
-\r
- //At least some Html strings match .NET Colors,\r
- // so this should work for those colors.\r
- // .NET colors, XWindows colors, and WWWC web colors \r
- // are (according to Charles Pretziod) base the same\r
- //colors, so many shouold work if any do.\r
- if (HtmlFromColor[0] != '#')\r
- {\r
- int length = HtmlFromColor.Length;\r
- for (int i = length - 1; i >= 0; i--)\r
- {\r
- if (!Char.IsDigit (HtmlFromColor[i]))\r
- break;\r
- length--;\r
- }\r
- \r
- return Color.FromName(HtmlFromColor.Substring (0, length));\r
- }\r
- \r
- int pos = 0, index = 0;\r
- int[] rgb = new int[] {0, 0, 0};\r
- \r
- string specifier = HtmlFromColor.Substring (1).ToLower ();\r
- if (specifier.Length != 6)\r
- return Color.Empty;\r
- \r
- foreach (char c in specifier)\r
- {\r
- rgb[index] *= 16;\r
-\r
- if (Char.IsDigit (c))\r
- rgb[index] += Int32.Parse (c.ToString ());\r
- else if (c <= 'f' && c >= 'a')\r
- rgb[index] += 10 + (c - 'a');\r
- else\r
- return Color.Empty;\r
- \r
- pos++;\r
- if ((pos % 2) == 0)\r
- index++;\r
- }\r
-\r
- return Color.FromArgb (rgb[0], rgb[1], rgb[2]);\r
- }\r
- \r
- /// <summary>\r
- /// \r
- /// </summary>\r
- /// <param name="OLEFromColor"></param>\r
- /// <returns></returns>\r
- public static Color FromOle(int OLEFromColor){\r
- //int newcolor;\r
- //TODO: swap RB bytes i.e. AARRGGBB to AABBGGRR\r
- //return Color.FromArgb(newcolor);\r
- return Color.Empty;\r
- }\r
- \r
- /// <summary>\r
- /// \r
- /// </summary>\r
- /// <param name="Win32FromColor"></param>\r
- /// <returns></returns>\r
- public static Color FromWin32(int Win32FromColor){\r
- //int newcolor;\r
- //TODO: swap RB bytes i.e. AARRGGBB to AABBGGRR\r
- //return Color.FromArgb(newcolor);\r
- return Color.Empty;\r
- }\r
-\r
- // To conversions\r
- public static string ToHtml (Color c)\r
- {\r
- if (c.IsEmpty)\r
- return "";\r
-\r
- string result;\r
-\r
- if (c.IsNamedColor)\r
- result = c.Name;\r
- else\r
- result = String.Format ("#{0:X2}{1:X2}{2:X2}", c.R, c.G, c.B);\r
-\r
- return result;\r
- }\r
- /// <summary>\r
- /// converts from BGR to RGB\r
- /// </summary>\r
- /// <param name="OleToColor"></param>\r
- /// <returns></returns>\r
- public static int ToOle(Color FromColor){\r
- // TODO: Swap red and blue(from argb), convert to int(toargb)\r
- // Same as ToWin32\r
- return (Color.FromArgb(FromColor.B,FromColor.G,FromColor.R)).ToArgb();\r
- }\r
-\r
- /// <summary>\r
- /// converts from RGB to BGR\r
- /// </summary>\r
- /// <param name="Win32ToColor"></param>\r
- /// <returns></returns>\r
- public static int ToWin32(Color FromColor){\r
- // TODO: Swap red and blue(from argb), convert to int(toargb)\r
- // Same as ToOle\r
- return (Color.FromArgb(FromColor.B,FromColor.G,FromColor.R)).ToArgb();\r
- }\r
- }\r
-}\r
-\r
-\r
-\r
-\r
+
+ public static Color FromHtml (string htmlColor)
+ {
+ if ((htmlColor == null) || (htmlColor.Length == 0))
+ return Color.Empty;
+
+ switch (htmlColor.ToLower ()) {
+ case "buttonface":
+ return SystemColors.Control;
+ case "captiontext":
+ return SystemColors.ActiveCaptionText;
+ case "threeddarkshadow":
+ return SystemColors.ControlDarkDark;
+ case "background":
+ return SystemColors.Desktop;
+ case "buttontext":
+ return SystemColors.ControlText;
+ case "infobackground":
+ return SystemColors.Info;
+ }
+ TypeConverter converter = TypeDescriptor.GetConverter (typeof (Color));
+ return (Color) converter.ConvertFromString (htmlColor);
+ }
+
+ public static Color FromOle (int oleColor)
+ {
+ // OleColor format is BGR
+ int R = oleColor & 0xFF;
+ int G = (oleColor >> 8) & 0xFF;
+ int B = (oleColor >> 16) & 0xFF;
+
+ Color retcolor = Color.FromArgb (255, R, G, B);
+ foreach (Color c in Color.NamedColors.Values) {
+ if (c == retcolor)
+ return c;
+ }
+
+ foreach (Color c in Color.SystemColors.Values) {
+ if (c == retcolor)
+ return c;
+ }
+
+ return retcolor;
+ }
+
+ public static Color FromWin32 (int win32Color)
+ {
+ // Win32Color format is BGR
+ int R = win32Color & 0xFF;
+ int G = (win32Color >> 8) & 0xFF;
+ int B = (win32Color >> 16) & 0xFF;
+
+ Color retcolor = Color.FromArgb (255, R, G, B);
+ foreach (Color c in Color.NamedColors.Values) {
+ if (c == retcolor)
+ return c;
+ }
+
+ foreach (Color c in Color.SystemColors.Values) {
+ if (c == retcolor)
+ return c;
+ }
+
+ return retcolor;
+ }
+
+ public static string ToHtml (Color c)
+ {
+ KnownColor kc;
+ if (c.IsEmpty)
+ return String.Empty;
+
+ string result;
+ if(c.IsSystemColor) {
+ kc = c.ToKnownColor();
+ switch (kc) {
+ case KnownColor.ActiveBorder:
+ return "activeborder";
+ case KnownColor.ActiveCaption:
+ return "activecaption";
+ case KnownColor.ActiveCaptionText:
+ return "captiontext";
+ case KnownColor.AppWorkspace:
+ return "appworkspace";
+ case KnownColor.Control:
+ return "buttonface";
+ case KnownColor.ControlDark:
+ return "buttonshadow";
+ case KnownColor.ControlDarkDark:
+ return "threeddarkshadow";
+ case KnownColor.ControlLight:
+ return "buttonface";
+ case KnownColor.ControlLightLight:
+ return "buttonhighlight";
+ case KnownColor.ControlText:
+ return "buttontext";
+ case KnownColor.Desktop:
+ return "background";
+ case KnownColor.GrayText:
+ return "graytext";
+ case KnownColor.Highlight:
+ case KnownColor.HotTrack:
+ return "highlight";
+ case KnownColor.HighlightText:
+ return "highlighttext";
+ case KnownColor.InactiveBorder:
+ return "inactiveborder";
+ case KnownColor.InactiveCaption:
+ return "inactivecaption";
+ case KnownColor.InactiveCaptionText:
+ return "inactivecaptiontext";
+ case KnownColor.Info:
+ return "infobackground";
+ case KnownColor.InfoText:
+ return "infotext";
+ case KnownColor.Menu:
+ return "menu";
+ case KnownColor.MenuText:
+ return "menutext";
+ case KnownColor.ScrollBar:
+ return "scrollbar";
+ case KnownColor.Window:
+ return "window";
+ case KnownColor.WindowFrame:
+ return "windowframe";
+ case KnownColor.WindowText:
+ return "windowtext";
+ default:
+ return String.Empty;
+ }
+ }
+
+ if (c.IsNamedColor) {
+ if (c == Color.LightGray) {
+ result = "LightGrey";
+ }
+ else
+ result = c.Name;
+ }
+ else
+ result = String.Format ("#{0:X2}{1:X2}{2:X2}", c.R, c.G, c.B);
+
+ return result;
+ }
+
+ public static int ToOle (Color c)
+ {
+ // OleColor format is BGR, same as Win32
+
+ return ((c.B << 16) | (c.G << 8) | c.R);
+ }
+
+ public static int ToWin32 (Color c)
+ {
+ // Win32Color format is BGR, Same as OleColor
+
+ return ((c.B << 16) | (c.G << 8) | c.R);
+ }
+ }
+}