PathGradientBrush.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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.Drawing;
  33. namespace System.Drawing.Drawing2D
  34. {
  35. /// <summary>
  36. /// Summary description for PathGradientBrush.
  37. /// </summary>
  38. public sealed class PathGradientBrush : Brush
  39. {
  40. internal PathGradientBrush (IntPtr native) : base (native)
  41. {
  42. }
  43. public PathGradientBrush (GraphicsPath path)
  44. {
  45. Status status = GDIPlus.GdipCreatePathGradientFromPath (path.NativeObject, out nativeObject);
  46. GDIPlus.CheckStatus (status);
  47. }
  48. public PathGradientBrush (Point [] points) : this (points, WrapMode.Clamp)
  49. {
  50. }
  51. public PathGradientBrush (PointF [] points) : this (points, WrapMode.Clamp)
  52. {
  53. }
  54. public PathGradientBrush (Point [] points, WrapMode wrapMode)
  55. {
  56. Status status = GDIPlus.GdipCreatePathGradientI (points, points.Length, wrapMode, out nativeObject);
  57. GDIPlus.CheckStatus (status);
  58. }
  59. public PathGradientBrush (PointF [] points, WrapMode wrapMode)
  60. {
  61. Status status = GDIPlus.GdipCreatePathGradient (points, points.Length, wrapMode, out nativeObject);
  62. GDIPlus.CheckStatus (status);
  63. }
  64. // Properties
  65. public Blend Blend {
  66. get {
  67. int count;
  68. Status status = GDIPlus.GdipGetPathGradientBlendCount (nativeObject, out count);
  69. GDIPlus.CheckStatus (status);
  70. float [] factors = new float [count];
  71. float [] positions = new float [count];
  72. status = GDIPlus.GdipGetPathGradientBlend (nativeObject, factors, positions, count);
  73. GDIPlus.CheckStatus (status);
  74. Blend blend = new Blend ();
  75. blend.Factors = factors;
  76. blend.Positions = positions;
  77. return blend;
  78. }
  79. set {
  80. int count;
  81. float [] factors = value.Factors;
  82. float [] positions = value.Positions;
  83. count = factors.Length;
  84. if (count == 0 || positions.Length == 0)
  85. throw new ArgumentException ("Invalid Blend object. It should have at least 2 elements in each of the factors and positions arrays.");
  86. if (count != positions.Length)
  87. throw new ArgumentException ("Invalid Blend object. It should contain the same number of factors and positions values.");
  88. if (positions [0] != 0.0F)
  89. throw new ArgumentException ("Invalid Blend object. The positions array must have 0.0 as its first element.");
  90. if (positions [count - 1] != 1.0F)
  91. throw new ArgumentException ("Invalid Blend object. The positions array must have 1.0 as its last element.");
  92. Status status = GDIPlus.GdipSetPathGradientBlend (nativeObject, factors, positions, count);
  93. GDIPlus.CheckStatus (status);
  94. }
  95. }
  96. public Color CenterColor {
  97. get {
  98. int centerColor;
  99. Status status = GDIPlus.GdipGetPathGradientCenterColor (nativeObject, out centerColor);
  100. GDIPlus.CheckStatus (status);
  101. return Color.FromArgb (centerColor);
  102. }
  103. set {
  104. Status status = GDIPlus.GdipSetPathGradientCenterColor (nativeObject, value.ToArgb ());
  105. GDIPlus.CheckStatus (status);
  106. }
  107. }
  108. public PointF CenterPoint {
  109. get {
  110. PointF center;
  111. Status status = GDIPlus.GdipGetPathGradientCenterPoint (nativeObject, out center);
  112. GDIPlus.CheckStatus (status);
  113. return center;
  114. }
  115. set {
  116. PointF center = value;
  117. Status status = GDIPlus.GdipSetPathGradientCenterPoint (nativeObject, ref center);
  118. GDIPlus.CheckStatus (status);
  119. }
  120. }
  121. public PointF FocusScales {
  122. get {
  123. float xScale;
  124. float yScale;
  125. Status status = GDIPlus.GdipGetPathGradientFocusScales (nativeObject, out xScale, out yScale);
  126. GDIPlus.CheckStatus (status);
  127. return new PointF (xScale, yScale);
  128. }
  129. set {
  130. Status status = GDIPlus.GdipSetPathGradientFocusScales (nativeObject, value.X, value.Y);
  131. GDIPlus.CheckStatus (status);
  132. }
  133. }
  134. public ColorBlend InterpolationColors {
  135. get {
  136. int count;
  137. Status status = GDIPlus.GdipGetPathGradientPresetBlendCount (nativeObject, out count);
  138. GDIPlus.CheckStatus (status);
  139. int [] intcolors = new int [count];
  140. float [] positions = new float [count];
  141. status = GDIPlus.GdipGetPathGradientPresetBlend (nativeObject, intcolors, positions, count);
  142. GDIPlus.CheckStatus (status);
  143. ColorBlend interpolationColors = new ColorBlend ();
  144. Color [] colors = new Color [count];
  145. for (int i = 0; i < count; i++)
  146. colors [i] = Color.FromArgb (intcolors [i]);
  147. interpolationColors.Colors = colors;
  148. interpolationColors.Positions = positions;
  149. return interpolationColors;
  150. }
  151. set {
  152. int count;
  153. Color [] colors = value.Colors;
  154. float [] positions = value.Positions;
  155. count = colors.Length;
  156. if (count == 0 || positions.Length == 0)
  157. throw new ArgumentException ("Invalid ColorBlend object. It should have at least 2 elements in each of the colors and positions arrays.");
  158. if (count != positions.Length)
  159. throw new ArgumentException ("Invalid ColorBlend object. It should contain the same number of positions and color values.");
  160. if (positions [0] != 0.0F)
  161. throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 0.0 as its first element.");
  162. if (positions [count - 1] != 1.0F)
  163. throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 1.0 as its last element.");
  164. int [] blend = new int [colors.Length];
  165. for (int i = 0; i < colors.Length; i++)
  166. blend [i] = colors [i].ToArgb ();
  167. Status status = GDIPlus.GdipSetPathGradientPresetBlend (nativeObject, blend, positions, count);
  168. GDIPlus.CheckStatus (status);
  169. }
  170. }
  171. public RectangleF Rectangle {
  172. get {
  173. RectangleF rect;
  174. Status status = GDIPlus.GdipGetPathGradientRect (nativeObject, out rect);
  175. GDIPlus.CheckStatus (status);
  176. return rect;
  177. }
  178. }
  179. public Color [] SurroundColors {
  180. get {
  181. int count;
  182. Status status = GDIPlus.GdipGetPathGradientSurroundColorCount (nativeObject, out count);
  183. GDIPlus.CheckStatus (status);
  184. int [] intcolors = new int [count];
  185. status = GDIPlus.GdipGetPathGradientSurroundColorsWithCount (nativeObject, intcolors, ref count);
  186. GDIPlus.CheckStatus (status);
  187. Color [] colors = new Color [count];
  188. for (int i = 0; i < count; i++)
  189. colors [i] = Color.FromArgb (intcolors [i]);
  190. return colors;
  191. }
  192. set {
  193. int count = value.Length;
  194. int [] colors = new int [count];
  195. for (int i = 0; i < count; i++)
  196. colors [i] = value [i].ToArgb ();
  197. Status status = GDIPlus.GdipSetPathGradientSurroundColorsWithCount (nativeObject, colors, ref count);
  198. GDIPlus.CheckStatus (status);
  199. }
  200. }
  201. public Matrix Transform {
  202. get {
  203. Matrix matrix = new Matrix ();
  204. Status status = GDIPlus.GdipGetPathGradientTransform (nativeObject, matrix.nativeMatrix);
  205. GDIPlus.CheckStatus (status);
  206. return matrix;
  207. }
  208. set {
  209. Status status = GDIPlus.GdipSetPathGradientTransform (nativeObject, value.nativeMatrix);
  210. GDIPlus.CheckStatus (status);
  211. }
  212. }
  213. public WrapMode WrapMode {
  214. get {
  215. WrapMode wrapMode;
  216. Status status = GDIPlus.GdipGetPathGradientWrapMode (nativeObject, out wrapMode);
  217. GDIPlus.CheckStatus (status);
  218. return wrapMode;
  219. }
  220. set {
  221. Status status = GDIPlus.GdipSetPathGradientWrapMode (nativeObject, value);
  222. GDIPlus.CheckStatus (status);
  223. }
  224. }
  225. // Methods
  226. public void MultiplyTransform (Matrix matrix)
  227. {
  228. MultiplyTransform (matrix, MatrixOrder.Prepend);
  229. }
  230. public void MultiplyTransform (Matrix matrix, MatrixOrder order)
  231. {
  232. Status status = GDIPlus.GdipMultiplyPathGradientTransform (nativeObject, matrix.nativeMatrix, order);
  233. GDIPlus.CheckStatus (status);
  234. }
  235. public void ResetTransform ()
  236. {
  237. Status status = GDIPlus.GdipResetPathGradientTransform (nativeObject);
  238. GDIPlus.CheckStatus (status);
  239. }
  240. public void RotateTransform (float angle)
  241. {
  242. RotateTransform (angle, MatrixOrder.Prepend);
  243. }
  244. public void RotateTransform (float angle, MatrixOrder order)
  245. {
  246. Status status = GDIPlus.GdipRotatePathGradientTransform (nativeObject, angle, order);
  247. GDIPlus.CheckStatus (status);
  248. }
  249. public void ScaleTransform (float sx, float sy)
  250. {
  251. ScaleTransform (sx, sy, MatrixOrder.Prepend);
  252. }
  253. public void ScaleTransform (float sx, float sy, MatrixOrder order)
  254. {
  255. Status status = GDIPlus.GdipScalePathGradientTransform (nativeObject, sx, sy, order);
  256. GDIPlus.CheckStatus (status);
  257. }
  258. public void SetBlendTriangularShape (float focus)
  259. {
  260. SetBlendTriangularShape (focus, 1.0F);
  261. }
  262. public void SetBlendTriangularShape (float focus, float scale)
  263. {
  264. if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
  265. throw new ArgumentException ("Invalid parameter passed.");
  266. Status status = GDIPlus.GdipSetPathGradientLinearBlend (nativeObject, focus, scale);
  267. GDIPlus.CheckStatus (status);
  268. }
  269. public void SetSigmaBellShape (float focus)
  270. {
  271. SetSigmaBellShape (focus, 1.0F);
  272. }
  273. public void SetSigmaBellShape (float focus, float scale)
  274. {
  275. if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
  276. throw new ArgumentException ("Invalid parameter passed.");
  277. Status status = GDIPlus.GdipSetPathGradientSigmaBlend (nativeObject, focus, scale);
  278. GDIPlus.CheckStatus (status);
  279. }
  280. public void TranslateTransform (float dx, float dy)
  281. {
  282. TranslateTransform (dx, dy, MatrixOrder.Prepend);
  283. }
  284. public void TranslateTransform (float dx, float dy, MatrixOrder order)
  285. {
  286. Status status = GDIPlus.GdipTranslatePathGradientTransform (nativeObject, dx, dy, order);
  287. GDIPlus.CheckStatus (status);
  288. }
  289. public override object Clone ()
  290. {
  291. IntPtr clonePtr;
  292. Status status = GDIPlus.GdipCloneBrush (nativeObject, out clonePtr);
  293. GDIPlus.CheckStatus (status);
  294. PathGradientBrush clone = new PathGradientBrush (clonePtr);
  295. return clone;
  296. }
  297. }
  298. }