**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Drawing / System.Drawing / ColorTranslator.cs
blobe5e8483422389dec3de0dc15fc42a54f017b60c9
1 //
2 // System.Drawing.ColorTranslator.cs
3 //
4 // Copyright (C) 2001 Ximian, Inc. http://www.ximian.com
5 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
6 //
7 // Authors:
8 // Dennis Hayes (dennish@raytek.com)
9 // Ravindra (rkumar@novell.com)
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 //
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 //
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System;
37 using System.ComponentModel;
39 namespace System.Drawing
41 public sealed class ColorTranslator
43 private ColorTranslator () { }
45 public static Color FromHtml (string HtmlFromColor)
47 TypeConverter converter = TypeDescriptor.GetConverter (typeof (Color));
48 return (Color) converter.ConvertFromString (HtmlFromColor);
51 public static Color FromOle (int OleFromColor)
53 // OleColor format is BGR
54 int R = OleFromColor & 0xFF;
55 int G = (OleFromColor >> 8) & 0xFF;
56 int B = (OleFromColor >> 16) & 0xFF;
58 Color retcolor = Color.FromArgb (255, R, G, B);
59 foreach (Color c in Color.NamedColors.Values) {
60 if (c == retcolor)
61 return c;
64 foreach (Color c in Color.SystemColors.Values) {
65 if (c == retcolor)
66 return c;
69 return retcolor;
72 public static Color FromWin32 (int Win32FromColor)
74 // Win32Color format is BGR
75 int R = Win32FromColor & 0xFF;
76 int G = (Win32FromColor >> 8) & 0xFF;
77 int B = (Win32FromColor >> 16) & 0xFF;
79 Color retcolor = Color.FromArgb (255, R, G, B);
80 foreach (Color c in Color.NamedColors.Values) {
81 if (c == retcolor)
82 return c;
85 foreach (Color c in Color.SystemColors.Values) {
86 if (c == retcolor)
87 return c;
90 return retcolor;
93 public static string ToHtml (Color c)
95 if (c.IsEmpty)
96 return "";
98 string result;
100 if (c.IsNamedColor)
101 result = c.Name;
102 else
103 result = String.Format ("#{0:X2}{1:X2}{2:X2}", c.R, c.G, c.B);
105 return result;
108 public static int ToOle (Color color)
110 // OleColor format is BGR, same as Win32
112 return ((color.B << 16) | (color.G << 8) | color.R);
115 public static int ToWin32 (Color color)
117 // Win32Color format is BGR, Same as OleColor
119 return ((color.B << 16) | (color.G << 8) | color.R);