ColorVectorConverter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.ComponentModel.Design.Serialization;
  5. using System.Globalization;
  6. using System.Reflection;
  7. namespace Microsoft.Drawing
  8. {
  9. /// <summary>
  10. /// 颜色向量转换类
  11. /// Copyright (c) JajaSoft
  12. /// </summary>
  13. public class ColorVectorConverter : TypeConverter
  14. {
  15. /// <summary>
  16. /// 返回该转换器是否可以使用指定的上下文将给定类型的对象转换为此转换器的类型。
  17. /// </summary>
  18. /// <param name="context">System.ComponentModel.ITypeDescriptorContext,提供格式上下文。</param>
  19. /// <param name="sourceType">一个 System.Type,表示要转换的类型。</param>
  20. /// <returns>如果该转换器能够执行转换,则为 true;否则为 false。</returns>
  21. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  22. {
  23. return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
  24. }
  25. /// <summary>
  26. /// 返回此转换器是否可以使用指定的上下文将该对象转换为指定的类型。
  27. /// </summary>
  28. /// <param name="context">System.ComponentModel.ITypeDescriptorContext,提供格式上下文。</param>
  29. /// <param name="destinationType">一个 System.Type,表示要转换到的类型。</param>
  30. /// <returns>如果该转换器能够执行转换,则为 true;否则为 false。</returns>
  31. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  32. {
  33. return ((destinationType == typeof(InstanceDescriptor)) || base.CanConvertTo(context, destinationType));
  34. }
  35. /// <summary>
  36. /// 使用指定的上下文和区域性信息将给定的对象转换为此转换器的类型。
  37. /// </summary>
  38. /// <param name="context">System.ComponentModel.ITypeDescriptorContext,提供格式上下文。</param>
  39. /// <param name="culture">用作当前区域性的 System.Globalization.CultureInfo。</param>
  40. /// <param name="value">要转换的 System.Object。</param>
  41. /// <returns>表示转换的 value 的 System.Object。</returns>
  42. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  43. {
  44. //NULL
  45. string str = value as string;
  46. if (str == null)
  47. return base.ConvertFrom(context, culture, value);
  48. //空字符串
  49. string trimStr = str.Trim();
  50. if (trimStr.Length <= 0)
  51. return ColorVector.Empty;
  52. //区域分隔符
  53. if (culture == null)
  54. culture = CultureInfo.CurrentCulture;
  55. char ch = culture.TextInfo.ListSeparator[0];
  56. //类型转换器
  57. TypeConverter longConverter = TypeDescriptor.GetConverter(typeof(long));
  58. TypeConverter intConverter = TypeDescriptor.GetConverter(typeof(int));
  59. //不含分隔符
  60. if (trimStr.IndexOf(ch) == -1)
  61. {
  62. if (((trimStr[0] == '#') &&
  63. (trimStr.Length == 13 || trimStr.Length == 17)) ||
  64. ((trimStr.StartsWith("0x") || trimStr.StartsWith("0X") || trimStr.StartsWith("&h") || trimStr.StartsWith("&H")) &&
  65. (trimStr.Length == 14 || trimStr.Length == 18)))
  66. return ColorVector.FromArgb((long)longConverter.ConvertFromString(trimStr));
  67. }
  68. else//包含分隔符
  69. {
  70. string[] strArray = trimStr.Split(new char[] { ch });
  71. int[] numArray = new int[strArray.Length];
  72. for (int i = 0; i < numArray.Length; i++)
  73. numArray[i] = (int)intConverter.ConvertFromString(context, culture, strArray[i]);
  74. switch (numArray.Length)
  75. {
  76. case 3:
  77. return ColorVector.FromArgb(numArray[0], numArray[1], numArray[2]);
  78. case 4:
  79. return ColorVector.FromArgb(numArray[0], numArray[1], numArray[2], numArray[3]);
  80. }
  81. }
  82. throw new ArgumentException("无效的颜色向量");
  83. }
  84. /// <summary>
  85. /// 使用指定的上下文和区域性信息将给定的值对象转换为指定的类型。
  86. /// </summary>
  87. /// <param name="context">System.ComponentModel.ITypeDescriptorContext,提供格式上下文。</param>
  88. /// <param name="culture">System.Globalization.CultureInfo。如果传递 null,则采用当前区域性。</param>
  89. /// <param name="value">要转换的 System.Object。</param>
  90. /// <param name="destinationType">value 参数要转换成的 System.Type。</param>
  91. /// <returns>表示转换的 value 的 System.Object。</returns>
  92. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  93. {
  94. if (destinationType == null)
  95. {
  96. throw new ArgumentNullException("destinationType");
  97. }
  98. if (value is ColorVector)
  99. {
  100. if (destinationType == typeof(string))//转换为string
  101. {
  102. ColorVector colorVector = (ColorVector)value;
  103. if (culture == null)
  104. culture = CultureInfo.CurrentCulture;
  105. string separator = culture.TextInfo.ListSeparator + " ";
  106. TypeConverter intConverter = TypeDescriptor.GetConverter(typeof(int));
  107. string[] strArray = new string[4];
  108. strArray[0] = intConverter.ConvertToString(context, culture, colorVector.A);
  109. strArray[1] = intConverter.ConvertToString(context, culture, colorVector.R);
  110. strArray[2] = intConverter.ConvertToString(context, culture, colorVector.G);
  111. strArray[3] = intConverter.ConvertToString(context, culture, colorVector.B);
  112. return string.Join(separator, strArray);
  113. }
  114. if (destinationType == typeof(InstanceDescriptor))
  115. {
  116. MemberInfo member = null;
  117. object[] arguments = null;
  118. ColorVector colorVector2 = (ColorVector)value;
  119. if (colorVector2 == ColorVector.Empty)
  120. {
  121. member = typeof(ColorVector).GetField("Empty");
  122. }
  123. else
  124. {
  125. member = typeof(ColorVector).GetMethod("FromArgb", new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) });
  126. arguments = new object[] { colorVector2.A, colorVector2.R, colorVector2.G, colorVector2.B };
  127. }
  128. if (member != null)
  129. return new InstanceDescriptor(member, arguments);
  130. }
  131. }
  132. return base.ConvertTo(context, culture, value, destinationType);
  133. }
  134. /// <summary>
  135. /// 在已知对象的属性 (Property) 值集的情况下,使用指定的上下文创建与此 System.ComponentModel.TypeConverter 关联的类型的实例。
  136. /// </summary>
  137. /// <param name="context">System.ComponentModel.ITypeDescriptorContext,提供格式上下文。</param>
  138. /// <param name="propertyValues">新属性 (Property) 值的 System.Collections.IDictionary。</param>
  139. /// <returns>一个 System.Object,表示给定的 System.Collections.IDictionary,或者如果无法创建该对象,则为 null。此方法始终返回 null。</returns>
  140. public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
  141. {
  142. if (propertyValues == null)
  143. {
  144. throw new ArgumentNullException("propertyValues");
  145. }
  146. object alpha = propertyValues["A"];
  147. object red = propertyValues["R"];
  148. object green = propertyValues["G"];
  149. object blue = propertyValues["B"];
  150. if (((alpha == null) || (red == null) || (green == null) || (blue == null))
  151. || (!(alpha is short) || !(red is short) || !(green is short) || !(blue is short)))
  152. {
  153. throw new ArgumentException("属性值为无效输入");
  154. }
  155. return ColorVector.FromArgb((int)(short)alpha, (int)(short)red, (int)(short)green, (int)(short)blue);
  156. }
  157. /// <summary>
  158. /// 返回有关更改该对象上的某个值是否需要使用指定的上下文调用 System.ComponentModel.TypeConverter.CreateInstance(System.Collections.IDictionary) 以创建新值的情况。
  159. /// </summary>
  160. /// <param name="context">System.ComponentModel.ITypeDescriptorContext,提供格式上下文。</param>
  161. /// <returns>如果更改此对象的属性需要调用 System.ComponentModel.TypeConverter.CreateInstance(System.Collections.IDictionary) 来创建新值,则为 true;否则为 false。</returns>
  162. public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
  163. {
  164. return true;
  165. }
  166. /// <summary>
  167. /// 使用指定的上下文返回值参数指定的数组类型的属性 (Property) 的集合。
  168. /// </summary>
  169. /// <param name="context">System.ComponentModel.ITypeDescriptorContext,提供格式上下文。</param>
  170. /// <param name="value">一个 System.Object,指定要为其获取属性的数组类型。</param>
  171. /// <param name="attributes">用作筛选器的 System.Attribute 类型数组。</param>
  172. /// <returns>具有为此数据类型公开的属性的 System.ComponentModel.PropertyDescriptorCollection;或者如果没有属性,则为 null。</returns>
  173. public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
  174. {
  175. return TypeDescriptor.GetProperties(typeof(ColorVector), attributes).Sort(new string[] { "A", "R", "G", "B" });
  176. }
  177. /// <summary>
  178. /// 返回此对象是否支持属性。
  179. /// </summary>
  180. /// <param name="context">System.ComponentModel.ITypeDescriptorContext,提供格式上下文。</param>
  181. /// <returns>如果应调用 System.ComponentModel.TypeConverter.GetProperties(System.Object) 来查找此对象的属性,则为 true;否则为 false。</returns>
  182. public override bool GetPropertiesSupported(ITypeDescriptorContext context)
  183. {
  184. return true;
  185. }
  186. }
  187. }