2 * Namespace: System.Web.UI.WebControls
\r
3 * Class: WebColorConverter
\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
11 * (C) Gaurav Vaish (2002)
\r
12 * (c) 2002 Ximian, Inc. (http://www.ximian.com)
\r
16 using System.Globalization;
\r
17 using System.ComponentModel;
\r
18 using System.Drawing;
\r
20 using System.Web.UI;
\r
22 namespace System.Web.UI.WebControls
\r
24 public class WebColorConverter : ColorConverter
\r
26 public override object ConvertFrom (ITypeDescriptorContext context,
\r
27 CultureInfo culture,
\r
30 if (value is string) {
\r
31 string val = ((string) value).Trim ();
\r
32 if(val == String.Empty || val.Length == 0)
\r
35 NumberStyles style = (val [0] == '#') ? NumberStyles.HexNumber :
\r
39 int n = Int32.Parse (val.Substring (1), style);
\r
40 return Color.FromArgb (n);
\r
42 Color c = Color.FromName (val);
\r
43 if (c.A != 0 || c.R != 0 || c.B != 0 || c.G != 0)
\r
48 return base.ConvertFrom(context, culture, value);
\r
51 public override object ConvertTo (ITypeDescriptorContext context,
\r
52 CultureInfo culture,
\r
54 Type destinationType)
\r
56 if (destinationType == null)
\r
57 throw new ArgumentNullException ("destinationType");
\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
64 if (c.IsNamedColor || c.IsSystemColor)
\r
67 return String.Format ("#{0:X2}{1:X2}{2:X2}{3:X2}", c.A, c.R, c.G, c.B);
\r
70 return base.ConvertTo(context, culture, value, destinationType);
\r