2 // System.Drawing.ColorTranslator.cs
\r
4 // (C) 2001 Ximian, Inc. http://www.ximian.com
\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
11 namespace System.Drawing {
\r
12 public class ColorTranslator{
\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
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
33 int length = HtmlFromColor.Length;
\r
34 for (int i = length - 1; i >= 0; i--)
\r
36 if (!Char.IsDigit (HtmlFromColor[i]))
\r
41 return Color.FromName(HtmlFromColor.Substring (0, length));
\r
44 int pos = 0, index = 0;
\r
45 int[] rgb = new int[] {0, 0, 0};
\r
47 string specifier = HtmlFromColor.Substring (1).ToLower ();
\r
48 if (specifier.Length != 6)
\r
51 foreach (char c in specifier)
\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
67 return Color.FromArgb (rgb[0], rgb[1], rgb[2]);
\r
73 /// <param name="OLEFromColor"></param>
\r
74 /// <returns></returns>
\r
75 public static Color FromOle(int OLEFromColor){
\r
77 //TODO: swap RB bytes i.e. AARRGGBB to AABBGGRR
\r
78 //return Color.FromArgb(newcolor);
\r
85 /// <param name="Win32FromColor"></param>
\r
86 /// <returns></returns>
\r
87 public static Color FromWin32(int Win32FromColor){
\r
89 //TODO: swap RB bytes i.e. AARRGGBB to AABBGGRR
\r
90 //return Color.FromArgb(newcolor);
\r
95 public static string ToHtml (Color c)
\r
102 if (c.IsNamedColor)
\r
105 result = String.Format ("#{0:X2}{1:X2}{2:X2}", c.R, c.G, c.B);
\r
110 /// converts from BGR to RGB
\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
117 return (Color.FromArgb(FromColor.B,FromColor.G,FromColor.R)).ToArgb();
\r
121 /// converts from RGB to BGR
\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
128 return (Color.FromArgb(FromColor.B,FromColor.G,FromColor.R)).ToArgb();
\r