New test.
[mono.git] / mcs / class / System.Drawing / System.Drawing / ColorTranslator.cs
index e5e8483422389dec3de0dc15fc42a54f017b60c9..c63708dc2565b1bc7ede7902226c9164c6bed28d 100644 (file)
@@ -1,17 +1,13 @@
 //
 // System.Drawing.ColorTranslator.cs
 //
-// Copyright (C) 2001 Ximian, Inc.  http://www.ximian.com
-// Copyright (C) 2004 Novell, Inc.  http://www.novell.com
-//
 // Authors:
 //     Dennis Hayes (dennish@raytek.com)
 //     Ravindra (rkumar@novell.com)
+//     Sebastien Pouliot  <sebastien@ximian.com>
 //
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.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
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
  
-using System;
 using System.ComponentModel;
 
-namespace System.Drawing
-{
-       public sealed class ColorTranslator
-       {
-               private ColorTranslator () { }
+namespace System.Drawing {
+
+       public sealed class ColorTranslator {
 
-               public static Color FromHtml (string HtmlFromColor)
+               private ColorTranslator ()
                {
+               }
+
+               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 (HtmlFromColor);
+                       return (Color) converter.ConvertFromString (htmlColor);
                }
 
-               public static Color FromOle (int OleFromColor)
+               public static Color FromOle (int oleColor)
                {
                        // OleColor format is BGR
-                       int R = OleFromColor & 0xFF;
-                       int G = (OleFromColor >> 8) & 0xFF;
-                       int B = (OleFromColor >> 16) & 0xFF;
+                       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) {
@@ -69,12 +83,12 @@ namespace System.Drawing
                        return retcolor;
                }
 
-               public static Color FromWin32 (int Win32FromColor)
+               public static Color FromWin32 (int win32Color)
                {
                        // Win32Color format is BGR
-                       int R = Win32FromColor & 0xFF;
-                       int G = (Win32FromColor >> 8) & 0xFF;
-                       int B = (Win32FromColor >> 16) & 0xFF;
+                       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) {
@@ -92,31 +106,95 @@ namespace System.Drawing
 
                public static string ToHtml (Color c)
                {
+                       KnownColor kc;
                        if (c.IsEmpty)
-                               return "";
+                               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)
-                               result = c.Name;
+                       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 color)
+               public static int ToOle (Color c)
                {
                        // OleColor format is BGR, same as Win32
 
-                       return  ((color.B << 16) | (color.G << 8) | color.R);
+                       return  ((c.B << 16) | (c.G << 8) | c.R);
                }
 
-               public static int ToWin32 (Color color)
+               public static int ToWin32 (Color c)
                {
                        // Win32Color format is BGR, Same as OleColor
 
-                       return  ((color.B << 16) | (color.G << 8) | color.R);
+                       return  ((c.B << 16) | (c.G << 8) | c.R);
                }
        }
 }