LinearGradientBrush.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //
  2. // System.Drawing.Drawing2D.LinearGradientBrush.cs
  3. //
  4. // Authors:
  5. // Dennis Hayes ([email protected])
  6. // Ravindra ([email protected])
  7. //
  8. // (C) 2002/3 Ximian, Inc. http://www.ximian.com
  9. // (C) 2004 Novell, Inc. http://www.novell.com
  10. //
  11. using System;
  12. namespace System.Drawing.Drawing2D
  13. {
  14. /// <summary>
  15. /// Summary description for LinearGradientBrush.
  16. /// </summary>
  17. public sealed class LinearGradientBrush : Brush
  18. {
  19. RectangleF rectangle;
  20. internal LinearGradientBrush (IntPtr native) : base (native)
  21. {
  22. }
  23. public LinearGradientBrush (Point point1, Point point2, Color color1, Color color2)
  24. {
  25. Status status = GDIPlus.GdipCreateLineBrushI (ref point1, ref point2, color1.ToArgb (), color2.ToArgb (), WrapMode.Tile, out nativeObject);
  26. GDIPlus.CheckStatus (status);
  27. Rectangle rect;
  28. status = GDIPlus.GdipGetLineRectI (nativeObject, out rect);
  29. GDIPlus.CheckStatus (status);
  30. rectangle = (RectangleF) rect;
  31. }
  32. public LinearGradientBrush (PointF point1, PointF point2, Color color1, Color color2)
  33. {
  34. Status status = GDIPlus.GdipCreateLineBrush (ref point1, ref point2, color1.ToArgb (), color2.ToArgb (), WrapMode.Tile, out nativeObject);
  35. GDIPlus.CheckStatus (status);
  36. status = GDIPlus.GdipGetLineRect (nativeObject, out rectangle);
  37. GDIPlus.CheckStatus (status);
  38. }
  39. public LinearGradientBrush (Rectangle rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
  40. {
  41. Status status = GDIPlus.GdipCreateLineBrushFromRectI (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
  42. GDIPlus.CheckStatus (status);
  43. rectangle = (RectangleF) rect;
  44. }
  45. public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle) : this (rect, color1, color2, angle, false)
  46. {
  47. }
  48. public LinearGradientBrush (RectangleF rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
  49. {
  50. Status status = GDIPlus.GdipCreateLineBrushFromRect (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
  51. GDIPlus.CheckStatus (status);
  52. rectangle = rect;
  53. }
  54. public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle) : this (rect, color1, color2, angle, false)
  55. {
  56. }
  57. public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle, bool isAngleScaleable)
  58. {
  59. Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngleI (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
  60. GDIPlus.CheckStatus (status);
  61. rectangle = (RectangleF) rect;
  62. }
  63. public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle, bool isAngleScaleable)
  64. {
  65. Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngle (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
  66. GDIPlus.CheckStatus (status);
  67. rectangle = rect;
  68. }
  69. // Public Properties
  70. public Blend Blend {
  71. get {
  72. int count;
  73. Status status = GDIPlus.GdipGetLineBlendCount (nativeObject, out count);
  74. GDIPlus.CheckStatus (status);
  75. float [] factors = new float [count];
  76. float [] positions = new float [count];
  77. status = GDIPlus.GdipGetLineBlend (nativeObject, factors, positions, count);
  78. GDIPlus.CheckStatus (status);
  79. Blend blend = new Blend ();
  80. blend.Factors = factors;
  81. blend.Positions = positions;
  82. return blend;
  83. }
  84. set {
  85. int count;
  86. float [] factors = value.Factors;
  87. float [] positions = value.Positions;
  88. count = factors.Length;
  89. if (count == 0 || positions.Length == 0)
  90. throw new ArgumentException ("Invalid Blend object. It should have at least 2 elements in each of the factors and positions arrays.");
  91. if (count != positions.Length)
  92. throw new ArgumentException ("Invalid Blend object. It should contain the same number of factors and positions values.");
  93. if (positions [0] != 0.0F)
  94. throw new ArgumentException ("Invalid Blend object. The positions array must have 0.0 as its first element.");
  95. if (positions [count - 1] != 1.0F)
  96. throw new ArgumentException ("Invalid Blend object. The positions array must have 1.0 as its last element.");
  97. Status status = GDIPlus.GdipSetLineBlend (nativeObject, factors, positions, count);
  98. GDIPlus.CheckStatus (status);
  99. }
  100. }
  101. public bool GammaCorrection {
  102. get {
  103. bool gammaCorrection;
  104. Status status = GDIPlus.GdipGetLineGammaCorrection (nativeObject, out gammaCorrection);
  105. GDIPlus.CheckStatus (status);
  106. return gammaCorrection;
  107. }
  108. set {
  109. Status status = GDIPlus.GdipSetLineGammaCorrection (nativeObject, value);
  110. GDIPlus.CheckStatus (status);
  111. }
  112. }
  113. public ColorBlend InterpolationColors {
  114. get {
  115. int count;
  116. Status status = GDIPlus.GdipGetLinePresetBlendCount (nativeObject, out count);
  117. GDIPlus.CheckStatus (status);
  118. int [] intcolors = new int [count];
  119. float [] positions = new float [count];
  120. status = GDIPlus.GdipGetLinePresetBlend (nativeObject, intcolors, positions, count);
  121. GDIPlus.CheckStatus (status);
  122. ColorBlend interpolationColors = new ColorBlend ();
  123. Color [] colors = new Color [count];
  124. for (int i = 0; i < count; i++)
  125. colors [i] = Color.FromArgb (intcolors [i]);
  126. interpolationColors.Colors = colors;
  127. interpolationColors.Positions = positions;
  128. return interpolationColors;
  129. }
  130. set {
  131. int count;
  132. Color [] colors = value.Colors;
  133. float [] positions = value.Positions;
  134. count = colors.Length;
  135. if (count == 0 || positions.Length == 0)
  136. throw new ArgumentException ("Invalid ColorBlend object. It should have at least 2 elements in each of the colors and positions arrays.");
  137. if (count != positions.Length)
  138. throw new ArgumentException ("Invalid ColorBlend object. It should contain the same number of positions and color values.");
  139. if (positions [0] != 0.0F)
  140. throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 0.0 as its first element.");
  141. if (positions [count - 1] != 1.0F)
  142. throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 1.0 as its last element.");
  143. int [] blend = new int [colors.Length];
  144. for (int i = 0; i < colors.Length; i++)
  145. blend [i] = colors [i].ToArgb ();
  146. Status status = GDIPlus.GdipSetLinePresetBlend (nativeObject, blend, positions, count);
  147. GDIPlus.CheckStatus (status);
  148. }
  149. }
  150. public Color [] LinearColors {
  151. get {
  152. int [] colors = new int [2];
  153. Status status = GDIPlus.GdipGetLineColors (nativeObject, colors);
  154. GDIPlus.CheckStatus (status);
  155. Color [] linearColors = new Color [2];
  156. linearColors [0] = Color.FromArgb (colors [0]);
  157. linearColors [1] = Color.FromArgb (colors [1]);
  158. return linearColors;
  159. }
  160. set {
  161. Status status = GDIPlus.GdipSetLineColors (nativeObject, value [0].ToArgb (), value [1].ToArgb ());
  162. GDIPlus.CheckStatus (status);
  163. }
  164. }
  165. public RectangleF Rectangle {
  166. get {
  167. return rectangle;
  168. }
  169. }
  170. public Matrix Transform {
  171. get {
  172. Matrix matrix = new Matrix ();
  173. Status status = GDIPlus.GdipGetLineTransform (nativeObject, matrix.nativeMatrix);
  174. GDIPlus.CheckStatus (status);
  175. return matrix;
  176. }
  177. set {
  178. Status status = GDIPlus.GdipSetLineTransform (nativeObject, value.nativeMatrix);
  179. GDIPlus.CheckStatus (status);
  180. }
  181. }
  182. public WrapMode WrapMode {
  183. get {
  184. WrapMode wrapMode;
  185. Status status = GDIPlus.GdipGetLineWrapMode (nativeObject, out wrapMode);
  186. GDIPlus.CheckStatus (status);
  187. return wrapMode;
  188. }
  189. set {
  190. Status status = GDIPlus.GdipSetLineWrapMode (nativeObject, value);
  191. GDIPlus.CheckStatus (status);
  192. }
  193. }
  194. // Public Methods
  195. public void MultiplyTransform (Matrix matrix)
  196. {
  197. MultiplyTransform (matrix, MatrixOrder.Prepend);
  198. }
  199. public void MultiplyTransform (Matrix matrix, MatrixOrder order)
  200. {
  201. Status status = GDIPlus.GdipMultiplyLineTransform (nativeObject, matrix.nativeMatrix, order);
  202. GDIPlus.CheckStatus (status);
  203. }
  204. public void ResetTransform ()
  205. {
  206. Status status = GDIPlus.GdipResetLineTransform (nativeObject);
  207. GDIPlus.CheckStatus (status);
  208. }
  209. public void RotateTransform (float angle)
  210. {
  211. RotateTransform (angle, MatrixOrder.Prepend);
  212. }
  213. public void RotateTransform (float angle, MatrixOrder order)
  214. {
  215. Status status = GDIPlus.GdipRotateLineTransform (nativeObject, angle, order);
  216. GDIPlus.CheckStatus (status);
  217. }
  218. public void ScaleTransform (float sx, float sy)
  219. {
  220. ScaleTransform (sx, sy, MatrixOrder.Prepend);
  221. }
  222. public void ScaleTransform (float sx, float sy, MatrixOrder order)
  223. {
  224. Status status = GDIPlus.GdipScaleLineTransform (nativeObject, sx, sy, order);
  225. GDIPlus.CheckStatus (status);
  226. }
  227. public void SetBlendTriangularShape (float focus)
  228. {
  229. SetBlendTriangularShape (focus, 1.0F);
  230. }
  231. public void SetBlendTriangularShape (float focus, float scale)
  232. {
  233. Status status = GDIPlus.GdipSetLineLinearBlend (nativeObject, focus, scale);
  234. GDIPlus.CheckStatus (status);
  235. }
  236. public void SetSigmaBellShape (float focus)
  237. {
  238. SetSigmaBellShape (focus, 1.0F);
  239. }
  240. public void SetSigmaBellShape (float focus, float scale)
  241. {
  242. Status status = GDIPlus.GdipSetLineSigmaBlend (nativeObject, focus, scale);
  243. GDIPlus.CheckStatus (status);
  244. }
  245. public void TranslateTransform (float dx, float dy)
  246. {
  247. TranslateTransform (dx, dy, MatrixOrder.Prepend);
  248. }
  249. public void TranslateTransform (float dx, float dy, MatrixOrder order)
  250. {
  251. Status status = GDIPlus.GdipTranslateLineTransform (nativeObject, dx, dy, order);
  252. GDIPlus.CheckStatus (status);
  253. }
  254. public override object Clone ()
  255. {
  256. IntPtr clonePtr;
  257. Status status = GDIPlus.GdipCloneBrush (nativeObject, out clonePtr);
  258. GDIPlus.CheckStatus (status);
  259. LinearGradientBrush clone = new LinearGradientBrush (clonePtr);
  260. return clone;
  261. }
  262. }
  263. }