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