WebColorConverter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok ([email protected])
  24. //
  25. //
  26. using System.Drawing;
  27. using System.Globalization;
  28. using System.ComponentModel;
  29. using System.Security.Permissions;
  30. namespace System.Web.UI.WebControls {
  31. // CAS
  32. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  33. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. public class WebColorConverter : ColorConverter {
  35. // Converts from string to Color
  36. public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
  37. {
  38. if (value is string)
  39. {
  40. string s;
  41. s = ((string)value).Trim();
  42. if (s.Length == 0)
  43. {
  44. return Color.Empty;
  45. }
  46. if (culture == null) {
  47. culture = CultureInfo.InvariantCulture;
  48. }
  49. if (s[0] == '#')
  50. {
  51. // Hex
  52. // MS throws a generic exception, wrapping the specific exception, who knows why...
  53. try
  54. {
  55. if (s.Length == 7)
  56. {
  57. int v;
  58. v = Int32.Parse(s.Substring(1), NumberStyles.HexNumber, culture);
  59. return Color.FromArgb(255, (v >> 16) & 0xff, (v >> 8) & 0xff, v & 0xff);
  60. }
  61. else
  62. {
  63. return Color.FromArgb(Int32.Parse(s.Substring(1), NumberStyles.HexNumber, culture));
  64. }
  65. }
  66. catch (FormatException e)
  67. {
  68. throw new Exception(s + "is not a valid color value", e);
  69. }
  70. catch (System.OverflowException e)
  71. {
  72. throw new Exception(s + " is not a valid color value", e);
  73. }
  74. }
  75. else
  76. {
  77. // Name or decimal
  78. int n = 0;
  79. try
  80. {
  81. n = Int32.Parse(s, NumberStyles.Integer, culture);
  82. }
  83. catch (FormatException e)
  84. {
  85. Color c;
  86. c = Color.FromName(s);
  87. if ((c.A != 0) || (c.R != 0) || (c.G != 0) || (c.B != 0))
  88. {
  89. return c;
  90. }
  91. throw new Exception(s + " is not a valid color value", e);
  92. }
  93. catch (System.OverflowException e)
  94. {
  95. throw new Exception(s + " is not a valid color value", e);
  96. }
  97. catch
  98. {
  99. throw;
  100. }
  101. return Color.FromArgb(n);
  102. }
  103. }
  104. return base.ConvertFrom (context, culture, value);
  105. }
  106. // Converts from Color to string
  107. public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  108. {
  109. if (!(value is Color) || destinationType != typeof (string))
  110. return base.ConvertTo (context, culture, value, destinationType);
  111. Color c = (Color) value;
  112. if (culture == null)
  113. culture = CultureInfo.InvariantCulture;
  114. string s = c.ToKnownColor ().ToString ();
  115. if (s != "0")
  116. return s;
  117. return String.Concat ("#", c.R.ToString ("X2"), c.G.ToString ("X2"), c.B.ToString ("X2"));
  118. }
  119. }
  120. }