WebColorConverter.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Namespace: System.Web.UI.WebControls
  23. * Class: WebColorConverter
  24. *
  25. * Author: Gaurav Vaish, Gonzalo Paniagua Javier
  26. * Maintainer: [email protected]
  27. * Contact: <[email protected]>, <[email protected]>, <[email protected]>
  28. * Implementation: yes
  29. * Status: 100%
  30. *
  31. * (C) Gaurav Vaish (2002)
  32. * (c) 2002 Ximian, Inc. (http://www.ximian.com)
  33. */
  34. using System;
  35. using System.Globalization;
  36. using System.ComponentModel;
  37. using System.Drawing;
  38. using System.Web;
  39. using System.Web.UI;
  40. namespace System.Web.UI.WebControls
  41. {
  42. public class WebColorConverter : ColorConverter
  43. {
  44. public override object ConvertFrom (ITypeDescriptorContext context,
  45. CultureInfo culture,
  46. object value)
  47. {
  48. if (value is string) {
  49. string val = ((string) value).Trim ();
  50. if(val == String.Empty || val.Length == 0)
  51. return Color.Empty;
  52. NumberStyles style = (val [0] == '#') ? NumberStyles.HexNumber :
  53. NumberStyles.None;
  54. try {
  55. int n = Int32.Parse (val.Substring (1), style);
  56. return Color.FromArgb (n);
  57. } catch {
  58. Color c = Color.FromName (val);
  59. if (c.A != 0 || c.R != 0 || c.B != 0 || c.G != 0)
  60. return c;
  61. }
  62. }
  63. return base.ConvertFrom(context, culture, value);
  64. }
  65. public override object ConvertTo (ITypeDescriptorContext context,
  66. CultureInfo culture,
  67. object value,
  68. Type destinationType)
  69. {
  70. if (destinationType == null)
  71. throw new ArgumentNullException ("destinationType");
  72. if (destinationType == typeof (String) && value != null) {
  73. Color c = (Color) value;
  74. if (c == Color.Empty)
  75. return String.Empty;
  76. if (c.IsNamedColor || c.IsSystemColor)
  77. return c.Name;
  78. return String.Format ("#{0:X2}{1:X2}{2:X2}{3:X2}", c.A, c.R, c.G, c.B);
  79. }
  80. return base.ConvertTo(context, culture, value, destinationType);
  81. }
  82. }
  83. }