2004-06-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / WebColorConverter.cs
1 /**\r
2  * Namespace: System.Web.UI.WebControls\r
3  * Class:     WebColorConverter\r
4  *\r
5  * Author:  Gaurav Vaish, Gonzalo Paniagua Javier\r
6  * Maintainer: gvaish@iitk.ac.in\r
7  * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>, <gonzalo@ximian.com>\r
8  * Implementation: yes\r
9  * Status:  100%\r
10  *\r
11  * (C) Gaurav Vaish (2002)\r
12  * (c) 2002 Ximian, Inc. (http://www.ximian.com)\r
13  */\r
14 \r
15 using System;\r
16 using System.Globalization;\r
17 using System.ComponentModel;\r
18 using System.Drawing;\r
19 using System.Web;\r
20 using System.Web.UI;\r
21 \r
22 namespace System.Web.UI.WebControls\r
23 {\r
24         public class WebColorConverter : ColorConverter\r
25         {\r
26                 public override object ConvertFrom (ITypeDescriptorContext context,\r
27                                                     CultureInfo culture,\r
28                                                     object value)\r
29                 {\r
30                         if (value is string) {\r
31                                 string val = ((string) value).Trim ();\r
32                                 if(val == String.Empty || val.Length == 0)\r
33                                         return Color.Empty;\r
34 \r
35                                 NumberStyles style = (val [0] == '#') ? NumberStyles.HexNumber :\r
36                                                                        NumberStyles.None;\r
37 \r
38                                 try {\r
39                                         int n = Int32.Parse (val.Substring (1), style);\r
40                                         return Color.FromArgb (n);\r
41                                 } catch {\r
42                                         Color c = Color.FromName (val);\r
43                                         if (c.A != 0 || c.R != 0 || c.B != 0 || c.G != 0)\r
44                                                 return c;\r
45                                 }\r
46                         }\r
47 \r
48                         return base.ConvertFrom(context, culture, value);\r
49                 }\r
50 \r
51                 public override object ConvertTo (ITypeDescriptorContext context,\r
52                                                   CultureInfo culture,\r
53                                                   object value,\r
54                                                   Type destinationType)\r
55                 {\r
56                         if (destinationType == null)\r
57                                 throw new ArgumentNullException ("destinationType");\r
58 \r
59                         if (destinationType == typeof (String) && value != null) {\r
60                                 Color c = (Color) value;\r
61                                 if (c == Color.Empty)\r
62                                         return String.Empty;\r
63 \r
64                                 if (c.IsNamedColor || c.IsSystemColor)\r
65                                         return c.Name;\r
66 \r
67                                 return String.Format ("#{0:X2}{1:X2}{2:X2}{3:X2}", c.A, c.R, c.G, c.B);\r
68                         }\r
69 \r
70                         return base.ConvertTo(context, culture, value, destinationType);\r
71                 }\r
72         }\r
73 }\r
74 \r