ColorConverter.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // System.Drawing.ColorConverter
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Ravindra ([email protected])
  7. //
  8. // Copyright (C) 2002 Ximian, Inc. http://www.ximian.com
  9. // Copyright (C) 2004 Novell, Inc. http://www.novell.com
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.Collections;
  35. using System.ComponentModel;
  36. using System.Globalization;
  37. using System.Text;
  38. using System.ComponentModel.Design.Serialization;
  39. using System.Reflection;
  40. namespace System.Drawing
  41. {
  42. public class ColorConverter : TypeConverter
  43. {
  44. static StandardValuesCollection cached;
  45. static object creatingCached = new object ();
  46. public ColorConverter () { }
  47. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  48. {
  49. if (sourceType == typeof (string))
  50. return true;
  51. return base.CanConvertFrom (context, sourceType);
  52. }
  53. public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
  54. {
  55. if (destinationType == typeof (InstanceDescriptor))
  56. return true;
  57. return base.CanConvertTo (context, destinationType);
  58. }
  59. public override object ConvertFrom (ITypeDescriptorContext context,
  60. CultureInfo culture,
  61. object value)
  62. {
  63. string s = value as string;
  64. if (s == null)
  65. return base.ConvertFrom (context, culture, value);
  66. s = s.Trim ();
  67. if (s.Length == 0) {
  68. return Color.Empty;
  69. }
  70. object named = Color.NamedColors [s];
  71. if (named != null)
  72. return (Color) named;
  73. named = Color.SystemColors [s];
  74. if (named != null)
  75. return (Color) named;
  76. String numSeparator = culture.TextInfo.ListSeparator;
  77. int A, R, G, B;
  78. if (s.IndexOf (numSeparator) > 0) { // "A, R, G, B" format
  79. String [] components = s.Split (numSeparator.ToCharArray ());
  80. if (components.Length == 3) {
  81. A = 255;
  82. R = GetNumber (components [0].Trim ());
  83. G = GetNumber (components [1].Trim ());
  84. B = GetNumber (components [2].Trim ());
  85. }
  86. else if (components.Length == 4) {
  87. A = GetNumber (components [0].Trim ());
  88. R = GetNumber (components [1].Trim ());
  89. G = GetNumber (components [2].Trim ());
  90. B = GetNumber (components [3].Trim ());
  91. }
  92. else
  93. throw new ArgumentException (s + " is not a valid color value.");
  94. }
  95. else { // #RRGGBB format
  96. int i = GetNumber (s.Trim ());
  97. A = (int) (i >> 24) & 0xFF;
  98. if (A == 0)
  99. A = 255;
  100. R = (i >> 16) & 0xFF;
  101. G = (i >> 8) & 0xFF;
  102. B = i & 0xFF;
  103. }
  104. Color result = Color.FromArgb (A, R, G, B);
  105. // Look for a named or system color with those values
  106. foreach (Color c in Color.NamedColors.Values) {
  107. if (c == result)
  108. return c;
  109. }
  110. foreach (Color c in Color.SystemColors.Values) {
  111. if (c == result)
  112. return c;
  113. }
  114. return result;
  115. }
  116. public override object ConvertTo (ITypeDescriptorContext context,
  117. CultureInfo culture,
  118. object value,
  119. Type destinationType)
  120. {
  121. if ((destinationType == typeof (string)) && (value is Color)) {
  122. Color color = (Color) value;
  123. if (color == Color.Empty) {
  124. return string.Empty;
  125. }
  126. if (color.IsKnownColor) {
  127. return color.Name;
  128. }
  129. if (color.IsNamedColor)
  130. return color.Name;
  131. String numSeparator = culture.TextInfo.ListSeparator;
  132. StringBuilder sb = new StringBuilder ();
  133. if (color.A != 255) {
  134. sb.Append (color.A);
  135. sb.Append (numSeparator);
  136. sb.Append (" ");
  137. }
  138. sb.Append (color.R);
  139. sb.Append (numSeparator);
  140. sb.Append (" ");
  141. sb.Append (color.G);
  142. sb.Append (numSeparator);
  143. sb.Append (" ");
  144. sb.Append (color.B);
  145. return sb.ToString ();
  146. }
  147. if (destinationType == typeof (InstanceDescriptor) && value is Color) {
  148. Color c = (Color)value;
  149. if (c.IsKnownColor){
  150. return new InstanceDescriptor (typeof (SystemColors).GetProperty (c.Name), null);
  151. } else {
  152. MethodInfo met = typeof(Color).GetMethod ("FromArgb", new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) } );
  153. return new InstanceDescriptor (met, new object[] {c.A, c.R, c.G, c.B });
  154. }
  155. }
  156. return base.ConvertTo (context, culture, value, destinationType);
  157. }
  158. public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
  159. {
  160. if (cached != null)
  161. return cached;
  162. lock (creatingCached)
  163. {
  164. if (cached != null)
  165. return cached;
  166. ICollection named = (ICollection) Color.NamedColors.Values;
  167. ICollection system = (ICollection) Color.SystemColors.Values;
  168. Array colors = Array.CreateInstance (typeof (Color), named.Count + system.Count);
  169. named.CopyTo (colors, 0);
  170. system.CopyTo (colors, named.Count);
  171. Array.Sort (colors, 0, colors.Length, new CompareColors ());
  172. cached = new StandardValuesCollection (colors);
  173. }
  174. return cached;
  175. }
  176. public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
  177. {
  178. return true;
  179. }
  180. private int GetNumber (String str)
  181. {
  182. int number;
  183. try {
  184. if (str.StartsWith ("#0x") || str.StartsWith ("#0X"))
  185. // #0xRRGGBB format. Parse hex string.
  186. number = Int32.Parse (str.Substring (3), NumberStyles.HexNumber);
  187. else if (str [0] == '#')
  188. // #RRGGBB format. Parse hex string.
  189. number = Int32.Parse (str.Substring (1), NumberStyles.HexNumber);
  190. else if (str.StartsWith ("0x") || str.StartsWith ("0X"))
  191. // 0xRRGGBB format. Parse hex string.
  192. number = Int32.Parse (str.Substring (2), NumberStyles.HexNumber);
  193. else // if (str [0] == '-' || str [0] == '+' || Char.IsDigit (str [0]))
  194. // [+/-]RRGGBB format. Parse decimal string.
  195. number = Int32.Parse (str, NumberStyles.Integer);
  196. return number;
  197. }
  198. catch (FormatException e) {
  199. throw new ArgumentException ("Can not convert this string to color: "+str, e);
  200. }
  201. }
  202. class CompareColors : IComparer
  203. {
  204. public int Compare (object x, object y)
  205. {
  206. return String.Compare (((Color) x).Name, ((Color) y).Name);
  207. }
  208. }
  209. }
  210. }