This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.Drawing / System.Drawing / ColorTranslator.cs
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)
10 //
11 //
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
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.
34 //
35  
36 using System;
37 using System.ComponentModel;
38
39 namespace System.Drawing
40 {
41         public sealed class ColorTranslator
42         {
43                 private ColorTranslator () { }
44
45                 public static Color FromHtml (string HtmlFromColor)
46                 {
47                         TypeConverter converter = TypeDescriptor.GetConverter (typeof (Color));
48                         return (Color) converter.ConvertFromString (HtmlFromColor);
49                 }
50
51                 public static Color FromOle (int OleFromColor)
52                 {
53                         // OleColor format is BGR
54                         int R = OleFromColor & 0xFF;
55                         int G = (OleFromColor >> 8) & 0xFF;
56                         int B = (OleFromColor >> 16) & 0xFF;
57
58                         Color retcolor = Color.FromArgb (255, R, G, B);
59                         foreach (Color c in Color.NamedColors.Values) {
60                                 if (c == retcolor)
61                                         return c;
62                         }
63
64                         foreach (Color c in Color.SystemColors.Values) {
65                                 if (c == retcolor)
66                                         return c;
67                         }
68
69                         return retcolor;
70                 }
71
72                 public static Color FromWin32 (int Win32FromColor)
73                 {
74                         // Win32Color format is BGR
75                         int R = Win32FromColor & 0xFF;
76                         int G = (Win32FromColor >> 8) & 0xFF;
77                         int B = (Win32FromColor >> 16) & 0xFF;
78
79                         Color retcolor = Color.FromArgb (255, R, G, B);
80                         foreach (Color c in Color.NamedColors.Values) {
81                                 if (c == retcolor)
82                                         return c;
83                         }
84
85                         foreach (Color c in Color.SystemColors.Values) {
86                                 if (c == retcolor)
87                                         return c;
88                         }
89
90                         return retcolor;
91                 }
92
93                 public static string ToHtml (Color c)
94                 {
95                         if (c.IsEmpty)
96                                 return "";
97
98                         string result;
99
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);
104
105                         return result;
106                 }
107
108                 public static int ToOle (Color color)
109                 {
110                         // OleColor format is BGR, same as Win32
111
112                         return  ((color.B << 16) | (color.G << 8) | color.R);
113                 }
114
115                 public static int ToWin32 (Color color)
116                 {
117                         // Win32Color format is BGR, Same as OleColor
118
119                         return  ((color.B << 16) | (color.G << 8) | color.R);
120                 }
121         }
122 }