DrawString implementation
[mono.git] / mcs / class / System.Drawing / System.Drawing / ColorTranslator.cs
1 //\r
2 // System.Drawing.ColorTranslator.cs\r
3 //\r
4 // (C) 2001 Ximian, Inc.  http://www.ximian.com\r
5 //\r
6 // Dennis Hayes (dennish@raytek.com)\r
7 // Inital Implimentation 3/25/2002\r
8 // All conversions based on best guess, will improve over time\r
9 // \r
10 using System;\r
11 namespace System.Drawing {\r
12         public class ColorTranslator{\r
13                 // From converisons\r
14                 /// <summary>\r
15                 /// \r
16                 /// </summary>\r
17                 /// <param name="HtmlFromColor"></param>\r
18                 /// <returns></returns>\r
19                 public static Color FromHtml(string HtmlFromColor){\r
20                         // If first char is "#"\r
21                                 //convert "#RRGGBB" to int and use Color.FromARGB(int) to create color\r
22                         // else //it is a color name\r
23                         //If there is a single digit at the end of the name, remove it.\r
24                         // Call Color.FromKnownColor(HtmlFromColor)  \r
25 \r
26                         //At least some Html strings match .NET Colors,\r
27                         // so this should work for those colors.\r
28                         // .NET colors, XWindows colors, and WWWC web colors \r
29                         // are (according to Charles Pretziod) base the same\r
30                         //colors, so many shouold work if any do.\r
31                         if (HtmlFromColor[0] != '#')\r
32                         {\r
33                                 int length = HtmlFromColor.Length;\r
34                                 for (int i = length - 1; i >= 0; i--)\r
35                                 {\r
36                                         if (!Char.IsDigit (HtmlFromColor[i]))\r
37                                                 break;\r
38                                         length--;\r
39                                 }\r
40                                 \r
41                                 return Color.FromName(HtmlFromColor.Substring (0, length));\r
42                         }\r
43                         \r
44                         int pos = 0, index = 0;\r
45                         int[] rgb = new int[] {0, 0, 0};\r
46                         \r
47                         string specifier = HtmlFromColor.Substring (1).ToLower ();\r
48                         if (specifier.Length != 6)\r
49                                 return Color.Empty;\r
50                                 \r
51                         foreach (char c in specifier)\r
52                         {\r
53                                 rgb[index] *= 16;\r
54 \r
55                                 if (Char.IsDigit (c))\r
56                                         rgb[index] += Int32.Parse (c.ToString ());\r
57                                 else if (c <= 'f' && c >= 'a')\r
58                                         rgb[index] += 10 + (c - 'a');\r
59                                 else\r
60                                         return Color.Empty;\r
61                                 \r
62                                 pos++;\r
63                                 if ((pos % 2) == 0)\r
64                                         index++;\r
65                         }\r
66 \r
67                         return Color.FromArgb (rgb[0], rgb[1], rgb[2]);\r
68                 }\r
69                 \r
70                 /// <summary>\r
71                 /// \r
72                 /// </summary>\r
73                 /// <param name="OLEFromColor"></param>\r
74                 /// <returns></returns>\r
75                 public static Color FromOle(int OLEFromColor){\r
76                         //int newcolor;\r
77                         //TODO: swap RB bytes i.e. AARRGGBB to AABBGGRR\r
78                         //return Color.FromArgb(newcolor);\r
79                         return Color.Empty;\r
80                 }\r
81                 \r
82                 /// <summary>\r
83                 /// \r
84                 /// </summary>\r
85                 /// <param name="Win32FromColor"></param>\r
86                 /// <returns></returns>\r
87                 public static Color FromWin32(int Win32FromColor){\r
88                         //int newcolor;\r
89                         //TODO: swap RB bytes i.e. AARRGGBB to AABBGGRR\r
90                         //return Color.FromArgb(newcolor);\r
91                         return Color.Empty;\r
92                 }\r
93 \r
94                 // To conversions\r
95                 public static string ToHtml (Color c)\r
96                 {\r
97                         if (c.IsEmpty)\r
98                                 return "";\r
99 \r
100                         string result;\r
101 \r
102                         if (c.IsNamedColor)\r
103                                 result = c.Name;\r
104                         else\r
105                                 result = String.Format ("#{0:X2}{1:X2}{2:X2}", c.R, c.G, c.B);\r
106 \r
107                         return result;\r
108                 }\r
109                 /// <summary>\r
110                 /// converts from BGR to RGB\r
111                 /// </summary>\r
112                 /// <param name="OleToColor"></param>\r
113                 /// <returns></returns>\r
114                 public static int ToOle(Color FromColor){\r
115                         // TODO: Swap red and blue(from argb), convert to int(toargb)\r
116                         // Same as ToWin32\r
117                         return (Color.FromArgb(FromColor.B,FromColor.G,FromColor.R)).ToArgb();\r
118                 }\r
119 \r
120                 /// <summary>\r
121                 /// converts from RGB to BGR\r
122                 /// </summary>\r
123                 /// <param name="Win32ToColor"></param>\r
124                 /// <returns></returns>\r
125                 public static int ToWin32(Color FromColor){\r
126                         // TODO: Swap red and blue(from argb), convert to int(toargb)\r
127                         // Same as ToOle\r
128                         return (Color.FromArgb(FromColor.B,FromColor.G,FromColor.R)).ToArgb();\r
129                 }\r
130         }\r
131 }\r
132 \r
133 \r
134 \r
135 \r