PathGradientBrush.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. // Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.ComponentModel;
  32. namespace System.Drawing.Drawing2D {
  33. [MonoTODO ("libgdiplus/cairo doesn't support path gradients - unless it can be mapped to a radial gradient")]
  34. public sealed class PathGradientBrush : Brush {
  35. internal PathGradientBrush (IntPtr native) : base (native)
  36. {
  37. }
  38. public PathGradientBrush (GraphicsPath path)
  39. {
  40. if (path == null)
  41. throw new ArgumentNullException ("path");
  42. Status status = GDIPlus.GdipCreatePathGradientFromPath (path.NativeObject, out nativeObject);
  43. GDIPlus.CheckStatus (status);
  44. }
  45. public PathGradientBrush (Point [] points) : this (points, WrapMode.Clamp)
  46. {
  47. }
  48. public PathGradientBrush (PointF [] points) : this (points, WrapMode.Clamp)
  49. {
  50. }
  51. public PathGradientBrush (Point [] points, WrapMode wrapMode)
  52. {
  53. if (points == null)
  54. throw new ArgumentNullException ("points");
  55. if ((wrapMode < WrapMode.Tile) || (wrapMode > WrapMode.Clamp))
  56. throw new InvalidEnumArgumentException ("WrapMode");
  57. Status status = GDIPlus.GdipCreatePathGradientI (points, points.Length, wrapMode, out nativeObject);
  58. GDIPlus.CheckStatus (status);
  59. }
  60. public PathGradientBrush (PointF [] points, WrapMode wrapMode)
  61. {
  62. if (points == null)
  63. throw new ArgumentNullException ("points");
  64. if ((wrapMode < WrapMode.Tile) || (wrapMode > WrapMode.Clamp))
  65. throw new InvalidEnumArgumentException ("WrapMode");
  66. Status status = GDIPlus.GdipCreatePathGradient (points, points.Length, wrapMode, out nativeObject);
  67. GDIPlus.CheckStatus (status);
  68. }
  69. // Properties
  70. public Blend Blend {
  71. get {
  72. int count;
  73. Status status = GDIPlus.GdipGetPathGradientBlendCount (nativeObject, out count);
  74. GDIPlus.CheckStatus (status);
  75. float [] factors = new float [count];
  76. float [] positions = new float [count];
  77. status = GDIPlus.GdipGetPathGradientBlend (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.GdipSetPathGradientBlend (nativeObject, factors, positions, count);
  98. GDIPlus.CheckStatus (status);
  99. }
  100. }
  101. public Color CenterColor {
  102. get {
  103. int centerColor;
  104. Status status = GDIPlus.GdipGetPathGradientCenterColor (nativeObject, out centerColor);
  105. GDIPlus.CheckStatus (status);
  106. return Color.FromArgb (centerColor);
  107. }
  108. set {
  109. Status status = GDIPlus.GdipSetPathGradientCenterColor (nativeObject, value.ToArgb ());
  110. GDIPlus.CheckStatus (status);
  111. }
  112. }
  113. public PointF CenterPoint {
  114. get {
  115. PointF center;
  116. Status status = GDIPlus.GdipGetPathGradientCenterPoint (nativeObject, out center);
  117. GDIPlus.CheckStatus (status);
  118. return center;
  119. }
  120. set {
  121. PointF center = value;
  122. Status status = GDIPlus.GdipSetPathGradientCenterPoint (nativeObject, ref center);
  123. GDIPlus.CheckStatus (status);
  124. }
  125. }
  126. public PointF FocusScales {
  127. get {
  128. float xScale;
  129. float yScale;
  130. Status status = GDIPlus.GdipGetPathGradientFocusScales (nativeObject, out xScale, out yScale);
  131. GDIPlus.CheckStatus (status);
  132. return new PointF (xScale, yScale);
  133. }
  134. set {
  135. Status status = GDIPlus.GdipSetPathGradientFocusScales (nativeObject, value.X, value.Y);
  136. GDIPlus.CheckStatus (status);
  137. }
  138. }
  139. public ColorBlend InterpolationColors {
  140. get {
  141. int count;
  142. Status status = GDIPlus.GdipGetPathGradientPresetBlendCount (nativeObject, out count);
  143. GDIPlus.CheckStatus (status);
  144. // if no failure, then the "managed" minimum is 1
  145. if (count < 1)
  146. count = 1;
  147. int [] intcolors = new int [count];
  148. float [] positions = new float [count];
  149. // status would fail if we ask points or types with a < 2 count
  150. if (count > 1) {
  151. status = GDIPlus.GdipGetPathGradientPresetBlend (nativeObject, intcolors, positions, count);
  152. GDIPlus.CheckStatus (status);
  153. }
  154. ColorBlend interpolationColors = new ColorBlend ();
  155. Color [] colors = new Color [count];
  156. for (int i = 0; i < count; i++)
  157. colors [i] = Color.FromArgb (intcolors [i]);
  158. interpolationColors.Colors = colors;
  159. interpolationColors.Positions = positions;
  160. return interpolationColors;
  161. }
  162. set {
  163. int count;
  164. Color [] colors = value.Colors;
  165. float [] positions = value.Positions;
  166. count = colors.Length;
  167. if (count == 0 || positions.Length == 0)
  168. throw new ArgumentException ("Invalid ColorBlend object. It should have at least 2 elements in each of the colors and positions arrays.");
  169. if (count != positions.Length)
  170. throw new ArgumentException ("Invalid ColorBlend object. It should contain the same number of positions and color values.");
  171. if (positions [0] != 0.0F)
  172. throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 0.0 as its first element.");
  173. if (positions [count - 1] != 1.0F)
  174. throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 1.0 as its last element.");
  175. int [] blend = new int [colors.Length];
  176. for (int i = 0; i < colors.Length; i++)
  177. blend [i] = colors [i].ToArgb ();
  178. Status status = GDIPlus.GdipSetPathGradientPresetBlend (nativeObject, blend, positions, count);
  179. GDIPlus.CheckStatus (status);
  180. }
  181. }
  182. public RectangleF Rectangle {
  183. get {
  184. RectangleF rect;
  185. Status status = GDIPlus.GdipGetPathGradientRect (nativeObject, out rect);
  186. GDIPlus.CheckStatus (status);
  187. return rect;
  188. }
  189. }
  190. public Color [] SurroundColors {
  191. get {
  192. int count;
  193. Status status = GDIPlus.GdipGetPathGradientSurroundColorCount (nativeObject, out count);
  194. GDIPlus.CheckStatus (status);
  195. int [] intcolors = new int [count];
  196. status = GDIPlus.GdipGetPathGradientSurroundColorsWithCount (nativeObject, intcolors, ref count);
  197. GDIPlus.CheckStatus (status);
  198. Color [] colors = new Color [count];
  199. for (int i = 0; i < count; i++)
  200. colors [i] = Color.FromArgb (intcolors [i]);
  201. return colors;
  202. }
  203. set {
  204. int count = value.Length;
  205. int [] colors = new int [count];
  206. for (int i = 0; i < count; i++)
  207. colors [i] = value [i].ToArgb ();
  208. Status status = GDIPlus.GdipSetPathGradientSurroundColorsWithCount (nativeObject, colors, ref count);
  209. GDIPlus.CheckStatus (status);
  210. }
  211. }
  212. public Matrix Transform {
  213. get {
  214. Matrix matrix = new Matrix ();
  215. Status status = GDIPlus.GdipGetPathGradientTransform (nativeObject, matrix.nativeMatrix);
  216. GDIPlus.CheckStatus (status);
  217. return matrix;
  218. }
  219. set {
  220. if (value == null)
  221. throw new ArgumentNullException ("Transform");
  222. Status status = GDIPlus.GdipSetPathGradientTransform (nativeObject, value.nativeMatrix);
  223. GDIPlus.CheckStatus (status);
  224. }
  225. }
  226. public WrapMode WrapMode {
  227. get {
  228. WrapMode wrapMode;
  229. Status status = GDIPlus.GdipGetPathGradientWrapMode (nativeObject, out wrapMode);
  230. GDIPlus.CheckStatus (status);
  231. return wrapMode;
  232. }
  233. set {
  234. if ((value < WrapMode.Tile) || (value > WrapMode.Clamp))
  235. throw new InvalidEnumArgumentException ("WrapMode");
  236. Status status = GDIPlus.GdipSetPathGradientWrapMode (nativeObject, value);
  237. GDIPlus.CheckStatus (status);
  238. }
  239. }
  240. // Methods
  241. public void MultiplyTransform (Matrix matrix)
  242. {
  243. MultiplyTransform (matrix, MatrixOrder.Prepend);
  244. }
  245. public void MultiplyTransform (Matrix matrix, MatrixOrder order)
  246. {
  247. if (matrix == null)
  248. throw new ArgumentNullException ("matrix");
  249. Status status = GDIPlus.GdipMultiplyPathGradientTransform (nativeObject, matrix.nativeMatrix, order);
  250. GDIPlus.CheckStatus (status);
  251. }
  252. public void ResetTransform ()
  253. {
  254. Status status = GDIPlus.GdipResetPathGradientTransform (nativeObject);
  255. GDIPlus.CheckStatus (status);
  256. }
  257. public void RotateTransform (float angle)
  258. {
  259. RotateTransform (angle, MatrixOrder.Prepend);
  260. }
  261. public void RotateTransform (float angle, MatrixOrder order)
  262. {
  263. Status status = GDIPlus.GdipRotatePathGradientTransform (nativeObject, angle, order);
  264. GDIPlus.CheckStatus (status);
  265. }
  266. public void ScaleTransform (float sx, float sy)
  267. {
  268. ScaleTransform (sx, sy, MatrixOrder.Prepend);
  269. }
  270. public void ScaleTransform (float sx, float sy, MatrixOrder order)
  271. {
  272. Status status = GDIPlus.GdipScalePathGradientTransform (nativeObject, sx, sy, order);
  273. GDIPlus.CheckStatus (status);
  274. }
  275. public void SetBlendTriangularShape (float focus)
  276. {
  277. SetBlendTriangularShape (focus, 1.0F);
  278. }
  279. public void SetBlendTriangularShape (float focus, float scale)
  280. {
  281. if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
  282. throw new ArgumentException ("Invalid parameter passed.");
  283. Status status = GDIPlus.GdipSetPathGradientLinearBlend (nativeObject, focus, scale);
  284. GDIPlus.CheckStatus (status);
  285. }
  286. public void SetSigmaBellShape (float focus)
  287. {
  288. SetSigmaBellShape (focus, 1.0F);
  289. }
  290. public void SetSigmaBellShape (float focus, float scale)
  291. {
  292. if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
  293. throw new ArgumentException ("Invalid parameter passed.");
  294. Status status = GDIPlus.GdipSetPathGradientSigmaBlend (nativeObject, focus, scale);
  295. GDIPlus.CheckStatus (status);
  296. }
  297. public void TranslateTransform (float dx, float dy)
  298. {
  299. TranslateTransform (dx, dy, MatrixOrder.Prepend);
  300. }
  301. public void TranslateTransform (float dx, float dy, MatrixOrder order)
  302. {
  303. Status status = GDIPlus.GdipTranslatePathGradientTransform (nativeObject, dx, dy, order);
  304. GDIPlus.CheckStatus (status);
  305. }
  306. public override object Clone ()
  307. {
  308. IntPtr clonePtr;
  309. Status status = GDIPlus.GdipCloneBrush (nativeObject, out clonePtr);
  310. GDIPlus.CheckStatus (status);
  311. PathGradientBrush clone = new PathGradientBrush (clonePtr);
  312. return clone;
  313. }
  314. }
  315. }