PathGradientBrush.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //
  2. // System.Drawing.Drawing2D.PathGradientBrush.cs
  3. //
  4. // Authors:
  5. // Dennis Hayes ([email protected])
  6. // Andreas Nahr ([email protected])
  7. // Ravindra ([email protected])
  8. //
  9. // Copyright (C) 2002/3 Ximian, Inc. http://www.ximian.com
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Drawing;
  34. using System.Runtime.InteropServices;
  35. namespace System.Drawing.Drawing2D
  36. {
  37. /// <summary>
  38. /// Summary description for PathGradientBrush.
  39. /// </summary>
  40. public sealed class PathGradientBrush : Brush
  41. {
  42. Blend blend;
  43. Color centerColor;
  44. PointF center;
  45. PointF focus;
  46. RectangleF rectangle;
  47. Color [] surroundColors;
  48. ColorBlend interpolationColors;
  49. Matrix transform;
  50. WrapMode wrapMode;
  51. internal PathGradientBrush (IntPtr native) : base (native)
  52. {
  53. }
  54. public PathGradientBrush (GraphicsPath path)
  55. {
  56. Status status = GDIPlus.GdipCreatePathGradientFromPath (path.NativeObject, out nativeObject);
  57. GDIPlus.CheckStatus (status);
  58. status = GDIPlus.GdipGetPathGradientRect (nativeObject, out rectangle);
  59. GDIPlus.CheckStatus (status);
  60. }
  61. public PathGradientBrush (Point [] points) : this (points, WrapMode.Clamp)
  62. {
  63. }
  64. public PathGradientBrush (PointF [] points) : this (points, WrapMode.Clamp)
  65. {
  66. }
  67. public PathGradientBrush (Point [] points, WrapMode wrapMode)
  68. {
  69. Status status = GDIPlus.GdipCreatePathGradientI (points, points.Length, wrapMode, out nativeObject);
  70. GDIPlus.CheckStatus (status);
  71. Rectangle rect;
  72. status = GDIPlus.GdipGetPathGradientRectI (nativeObject, out rect);
  73. GDIPlus.CheckStatus (status);
  74. rectangle = (RectangleF) rect;
  75. }
  76. public PathGradientBrush (PointF [] points, WrapMode wrapMode)
  77. {
  78. Status status = GDIPlus.GdipCreatePathGradient (points, points.Length, wrapMode, out nativeObject);
  79. GDIPlus.CheckStatus (status);
  80. status = GDIPlus.GdipGetPathGradientRect (nativeObject, out rectangle);
  81. GDIPlus.CheckStatus (status);
  82. }
  83. // Properties
  84. public Blend Blend {
  85. get {
  86. return blend;
  87. }
  88. set {
  89. Status status = GDIPlus.GdipSetPathGradientBlend (nativeObject, value.Factors, value.Positions, value.Factors.Length);
  90. GDIPlus.CheckStatus (status);
  91. blend = value;
  92. }
  93. }
  94. public Color CenterColor {
  95. get {
  96. return centerColor;
  97. }
  98. set {
  99. Status status = GDIPlus.GdipSetPathGradientCenterColor (nativeObject, value.ToArgb ());
  100. GDIPlus.CheckStatus (status);
  101. centerColor = value;
  102. }
  103. }
  104. public PointF CenterPoint {
  105. get {
  106. return center;
  107. }
  108. set {
  109. Status status = GDIPlus.GdipSetPathGradientCenterPoint (nativeObject, ref value);
  110. GDIPlus.CheckStatus (status);
  111. center = value;
  112. }
  113. }
  114. public PointF FocusScales {
  115. get {
  116. return focus;
  117. }
  118. set {
  119. Status status = GDIPlus.GdipSetPathGradientFocusScales (nativeObject, value.X, value.Y);
  120. GDIPlus.CheckStatus (status);
  121. focus = value;
  122. }
  123. }
  124. public ColorBlend InterpolationColors {
  125. get {
  126. return interpolationColors;
  127. }
  128. set {
  129. Color [] colors = value.Colors;
  130. int [] blend = new int [colors.Length];
  131. for (int i = 0; i < colors.Length; i++)
  132. blend [i] = colors [i].ToArgb ();
  133. Status status = GDIPlus.GdipSetPathGradientPresetBlend (nativeObject, blend, value.Positions, blend.Length);
  134. GDIPlus.CheckStatus (status);
  135. interpolationColors = value;
  136. }
  137. }
  138. public RectangleF Rectangle {
  139. get {
  140. return rectangle;
  141. }
  142. }
  143. public Color [] SurroundColors {
  144. get {
  145. return surroundColors;
  146. }
  147. set {
  148. int length = value.Length;
  149. int [] colors = new int [length];
  150. for (int i = 0; i < length; i++)
  151. colors [i] = value [i].ToArgb ();
  152. Status status = GDIPlus.GdipSetPathGradientSurroundColorsWithCount (nativeObject, colors, ref length);
  153. GDIPlus.CheckStatus (status);
  154. surroundColors = value;
  155. }
  156. }
  157. public Matrix Transform {
  158. get {
  159. return transform;
  160. }
  161. set {
  162. Status status = GDIPlus.GdipSetPathGradientTransform (nativeObject, value.nativeMatrix);
  163. GDIPlus.CheckStatus (status);
  164. transform = value;
  165. }
  166. }
  167. public WrapMode WrapMode {
  168. get {
  169. return wrapMode;
  170. }
  171. set {
  172. Status status = GDIPlus.GdipSetPathGradientWrapMode (nativeObject, value);
  173. GDIPlus.CheckStatus (status);
  174. wrapMode = value;
  175. }
  176. }
  177. // Methods
  178. public void MultiplyTransform (Matrix matrix)
  179. {
  180. MultiplyTransform (matrix, MatrixOrder.Prepend);
  181. }
  182. public void MultiplyTransform (Matrix matrix, MatrixOrder order)
  183. {
  184. Status status = GDIPlus.GdipMultiplyPathGradientTransform (nativeObject, matrix.nativeMatrix, order);
  185. GDIPlus.CheckStatus (status);
  186. }
  187. public void ResetTransform ()
  188. {
  189. Status status = GDIPlus.GdipResetPathGradientTransform (nativeObject);
  190. GDIPlus.CheckStatus (status);
  191. }
  192. public void RotateTransform (float angle)
  193. {
  194. RotateTransform (angle, MatrixOrder.Prepend);
  195. }
  196. public void RotateTransform (float angle, MatrixOrder order)
  197. {
  198. Status status = GDIPlus.GdipRotatePathGradientTransform (nativeObject, angle, order);
  199. GDIPlus.CheckStatus (status);
  200. }
  201. public void ScaleTransform (float sx, float sy)
  202. {
  203. ScaleTransform (sx, sy, MatrixOrder.Prepend);
  204. }
  205. public void ScaleTransform (float sx, float sy, MatrixOrder order)
  206. {
  207. Status status = GDIPlus.GdipScalePathGradientTransform (nativeObject, sx, sy, order);
  208. GDIPlus.CheckStatus (status);
  209. }
  210. public void SetBlendTriangularShape (float focus)
  211. {
  212. SetBlendTriangularShape (focus, 1.0F);
  213. }
  214. public void SetBlendTriangularShape (float focus, float scale)
  215. {
  216. if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
  217. throw new ArgumentException ("Invalid parameter passed.");
  218. Status status = GDIPlus.GdipSetPathGradientLinearBlend (nativeObject, focus, scale);
  219. GDIPlus.CheckStatus (status);
  220. }
  221. public void SetSigmaBellShape (float focus)
  222. {
  223. SetSigmaBellShape (focus, 1.0F);
  224. }
  225. public void SetSigmaBellShape (float focus, float scale)
  226. {
  227. if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
  228. throw new ArgumentException ("Invalid parameter passed.");
  229. Status status = GDIPlus.GdipSetPathGradientSigmaBlend (nativeObject, focus, scale);
  230. GDIPlus.CheckStatus (status);
  231. }
  232. public void TranslateTransform (float dx, float dy)
  233. {
  234. TranslateTransform (dx, dy, MatrixOrder.Prepend);
  235. }
  236. public void TranslateTransform (float dx, float dy, MatrixOrder order)
  237. {
  238. Status status = GDIPlus.GdipTranslatePathGradientTransform (nativeObject, dx, dy, order);
  239. GDIPlus.CheckStatus (status);
  240. }
  241. public override object Clone ()
  242. {
  243. IntPtr clonePtr;
  244. Status status = GDIPlus.GdipCloneBrush (nativeObject, out clonePtr);
  245. GDIPlus.CheckStatus (status);
  246. PathGradientBrush clone = new PathGradientBrush (clonePtr);
  247. return clone;
  248. }
  249. }
  250. }