LinearGradientBrush.cs 11 KB

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