2 // System.Drawing.ColorTranslator.cs
4 // (C) 2001 Ximian, Inc. http://www.ximian.com
6 // Dennis Hayes (dennish@raytek.com)
7 // Inital Implimentation 3/25/2002
8 // All conversions based on best guess, will improve over time
11 namespace System.Drawing {
12 public sealed class ColorTranslator{
14 private ColorTranslator ()
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)
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] != '#')
38 int length = HtmlFromColor.Length;
39 for (int i = length - 1; i >= 0; i--)
41 if (!Char.IsDigit (HtmlFromColor[i]))
46 return Color.FromName(HtmlFromColor.Substring (0, length));
49 int pos = 0, index = 0;
50 int[] rgb = new int[] {0, 0, 0};
52 string specifier = HtmlFromColor.Substring (1).ToLower ();
53 if (specifier.Length != 6)
56 foreach (char c in specifier)
61 rgb[index] += Int32.Parse (c.ToString ());
62 else if (c <= 'f' && c >= 'a')
63 rgb[index] += 10 + (c - 'a');
72 return Color.FromArgb (rgb[0], rgb[1], rgb[2]);
78 /// <param name="OLEFromColor"></param>
79 /// <returns></returns>
80 public static Color FromOle(int OLEFromColor){
82 //TODO: swap RB bytes i.e. AARRGGBB to AABBGGRR
83 //return Color.FromArgb(newcolor);
90 /// <param name="Win32FromColor"></param>
91 /// <returns></returns>
92 public static Color FromWin32(int Win32FromColor){
94 //TODO: swap RB bytes i.e. AARRGGBB to AABBGGRR
95 //return Color.FromArgb(newcolor);
100 public static string ToHtml (Color c)
110 result = String.Format ("#{0:X2}{1:X2}{2:X2}", c.R, c.G, c.B);
115 /// converts from BGR to RGB
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)
122 return (Color.FromArgb(FromColor.B,FromColor.G,FromColor.R)).ToArgb();
126 /// converts from RGB to BGR
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)
133 return (Color.FromArgb(FromColor.B,FromColor.G,FromColor.R)).ToArgb();