2
0

LinearGradientBrush.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. // Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.ComponentModel;
  31. namespace System.Drawing.Drawing2D {
  32. public sealed class LinearGradientBrush : Brush
  33. {
  34. RectangleF rectangle;
  35. internal LinearGradientBrush (IntPtr native) : base (native)
  36. {
  37. Status status = GDIPlus.GdipGetLineRect (native, out rectangle);
  38. GDIPlus.CheckStatus (status);
  39. }
  40. public LinearGradientBrush (Point point1, Point point2, Color color1, Color color2)
  41. {
  42. Status status = GDIPlus.GdipCreateLineBrushI (ref point1, ref point2, color1.ToArgb (), color2.ToArgb (), WrapMode.Tile, out nativeObject);
  43. GDIPlus.CheckStatus (status);
  44. status = GDIPlus.GdipGetLineRect (nativeObject, out rectangle);
  45. GDIPlus.CheckStatus (status);
  46. }
  47. public LinearGradientBrush (PointF point1, PointF point2, Color color1, Color color2)
  48. {
  49. Status status = GDIPlus.GdipCreateLineBrush (ref point1, ref point2, color1.ToArgb (), color2.ToArgb (), WrapMode.Tile, out nativeObject);
  50. GDIPlus.CheckStatus (status);
  51. status = GDIPlus.GdipGetLineRect (nativeObject, out rectangle);
  52. GDIPlus.CheckStatus (status);
  53. }
  54. public LinearGradientBrush (Rectangle rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
  55. {
  56. Status status = GDIPlus.GdipCreateLineBrushFromRectI (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
  57. GDIPlus.CheckStatus (status);
  58. rectangle = (RectangleF) rect;
  59. }
  60. public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle) : this (rect, color1, color2, angle, false)
  61. {
  62. }
  63. public LinearGradientBrush (RectangleF rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
  64. {
  65. Status status = GDIPlus.GdipCreateLineBrushFromRect (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
  66. GDIPlus.CheckStatus (status);
  67. rectangle = rect;
  68. }
  69. public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle) : this (rect, color1, color2, angle, false)
  70. {
  71. }
  72. public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle, bool isAngleScaleable)
  73. {
  74. Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngleI (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
  75. GDIPlus.CheckStatus (status);
  76. rectangle = (RectangleF) rect;
  77. }
  78. public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle, bool isAngleScaleable)
  79. {
  80. Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngle (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
  81. GDIPlus.CheckStatus (status);
  82. rectangle = rect;
  83. }
  84. // Public Properties
  85. public Blend Blend {
  86. get {
  87. int count;
  88. Status status = GDIPlus.GdipGetLineBlendCount (nativeObject, out count);
  89. GDIPlus.CheckStatus (status);
  90. float [] factors = new float [count];
  91. float [] positions = new float [count];
  92. status = GDIPlus.GdipGetLineBlend (nativeObject, factors, positions, count);
  93. GDIPlus.CheckStatus (status);
  94. Blend blend = new Blend ();
  95. blend.Factors = factors;
  96. blend.Positions = positions;
  97. return blend;
  98. }
  99. set {
  100. int count;
  101. float [] factors = value.Factors;
  102. float [] positions = value.Positions;
  103. count = factors.Length;
  104. if (count == 0 || positions.Length == 0)
  105. throw new ArgumentException ("Invalid Blend object. It should have at least 2 elements in each of the factors and positions arrays.");
  106. if (count != positions.Length)
  107. throw new ArgumentException ("Invalid Blend object. It should contain the same number of factors and positions values.");
  108. if (positions [0] != 0.0F)
  109. throw new ArgumentException ("Invalid Blend object. The positions array must have 0.0 as its first element.");
  110. if (positions [count - 1] != 1.0F)
  111. throw new ArgumentException ("Invalid Blend object. The positions array must have 1.0 as its last element.");
  112. Status status = GDIPlus.GdipSetLineBlend (nativeObject, factors, positions, count);
  113. GDIPlus.CheckStatus (status);
  114. }
  115. }
  116. [MonoTODO ("Not used inside libgdiplus")]
  117. public bool GammaCorrection {
  118. get {
  119. bool gammaCorrection;
  120. Status status = GDIPlus.GdipGetLineGammaCorrection (nativeObject, out gammaCorrection);
  121. GDIPlus.CheckStatus (status);
  122. return gammaCorrection;
  123. }
  124. set {
  125. Status status = GDIPlus.GdipSetLineGammaCorrection (nativeObject, value);
  126. GDIPlus.CheckStatus (status);
  127. }
  128. }
  129. public ColorBlend InterpolationColors {
  130. get {
  131. int count;
  132. Status status = GDIPlus.GdipGetLinePresetBlendCount (nativeObject, out count);
  133. GDIPlus.CheckStatus (status);
  134. int [] intcolors = new int [count];
  135. float [] positions = new float [count];
  136. status = GDIPlus.GdipGetLinePresetBlend (nativeObject, intcolors, positions, count);
  137. GDIPlus.CheckStatus (status);
  138. ColorBlend interpolationColors = new ColorBlend ();
  139. Color [] colors = new Color [count];
  140. for (int i = 0; i < count; i++)
  141. colors [i] = Color.FromArgb (intcolors [i]);
  142. interpolationColors.Colors = colors;
  143. interpolationColors.Positions = positions;
  144. return interpolationColors;
  145. }
  146. set {
  147. int count;
  148. Color [] colors = value.Colors;
  149. float [] positions = value.Positions;
  150. count = colors.Length;
  151. if (count == 0 || positions.Length == 0)
  152. throw new ArgumentException ("Invalid ColorBlend object. It should have at least 2 elements in each of the colors and positions arrays.");
  153. if (count != positions.Length)
  154. throw new ArgumentException ("Invalid ColorBlend object. It should contain the same number of positions and color values.");
  155. if (positions [0] != 0.0F)
  156. throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 0.0 as its first element.");
  157. if (positions [count - 1] != 1.0F)
  158. throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 1.0 as its last element.");
  159. int [] blend = new int [colors.Length];
  160. for (int i = 0; i < colors.Length; i++)
  161. blend [i] = colors [i].ToArgb ();
  162. Status status = GDIPlus.GdipSetLinePresetBlend (nativeObject, blend, positions, count);
  163. GDIPlus.CheckStatus (status);
  164. }
  165. }
  166. public Color [] LinearColors {
  167. get {
  168. int [] colors = new int [2];
  169. Status status = GDIPlus.GdipGetLineColors (nativeObject, colors);
  170. GDIPlus.CheckStatus (status);
  171. Color [] linearColors = new Color [2];
  172. linearColors [0] = Color.FromArgb (colors [0]);
  173. linearColors [1] = Color.FromArgb (colors [1]);
  174. return linearColors;
  175. }
  176. set {
  177. Status status = GDIPlus.GdipSetLineColors (nativeObject, value [0].ToArgb (), value [1].ToArgb ());
  178. GDIPlus.CheckStatus (status);
  179. }
  180. }
  181. public RectangleF Rectangle {
  182. get {
  183. return rectangle;
  184. }
  185. }
  186. public Matrix Transform {
  187. get {
  188. Matrix matrix = new Matrix ();
  189. Status status = GDIPlus.GdipGetLineTransform (nativeObject, matrix.nativeMatrix);
  190. GDIPlus.CheckStatus (status);
  191. return matrix;
  192. }
  193. set {
  194. if (value == null)
  195. throw new ArgumentNullException ("Transform");
  196. Status status = GDIPlus.GdipSetLineTransform (nativeObject, value.nativeMatrix);
  197. GDIPlus.CheckStatus (status);
  198. }
  199. }
  200. public WrapMode WrapMode {
  201. get {
  202. WrapMode wrapMode;
  203. Status status = GDIPlus.GdipGetLineWrapMode (nativeObject, out wrapMode);
  204. GDIPlus.CheckStatus (status);
  205. return wrapMode;
  206. }
  207. set {
  208. // note: Clamp isn't valid (context wise) but it is checked in libgdiplus
  209. if ((value < WrapMode.Tile) || (value > WrapMode.Clamp))
  210. throw new InvalidEnumArgumentException ("WrapMode");
  211. Status status = GDIPlus.GdipSetLineWrapMode (nativeObject, value);
  212. GDIPlus.CheckStatus (status);
  213. }
  214. }
  215. // Public Methods
  216. public void MultiplyTransform (Matrix matrix)
  217. {
  218. MultiplyTransform (matrix, MatrixOrder.Prepend);
  219. }
  220. public void MultiplyTransform (Matrix matrix, MatrixOrder order)
  221. {
  222. if (matrix == null)
  223. throw new ArgumentNullException ("matrix");
  224. Status status = GDIPlus.GdipMultiplyLineTransform (nativeObject, matrix.nativeMatrix, order);
  225. GDIPlus.CheckStatus (status);
  226. }
  227. public void ResetTransform ()
  228. {
  229. Status status = GDIPlus.GdipResetLineTransform (nativeObject);
  230. GDIPlus.CheckStatus (status);
  231. }
  232. public void RotateTransform (float angle)
  233. {
  234. RotateTransform (angle, MatrixOrder.Prepend);
  235. }
  236. public void RotateTransform (float angle, MatrixOrder order)
  237. {
  238. Status status = GDIPlus.GdipRotateLineTransform (nativeObject, angle, order);
  239. GDIPlus.CheckStatus (status);
  240. }
  241. public void ScaleTransform (float sx, float sy)
  242. {
  243. ScaleTransform (sx, sy, MatrixOrder.Prepend);
  244. }
  245. public void ScaleTransform (float sx, float sy, MatrixOrder order)
  246. {
  247. Status status = GDIPlus.GdipScaleLineTransform (nativeObject, sx, sy, order);
  248. GDIPlus.CheckStatus (status);
  249. }
  250. public void SetBlendTriangularShape (float focus)
  251. {
  252. SetBlendTriangularShape (focus, 1.0F);
  253. }
  254. public void SetBlendTriangularShape (float focus, float scale)
  255. {
  256. if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
  257. throw new ArgumentException ("Invalid parameter passed.");
  258. Status status = GDIPlus.GdipSetLineLinearBlend (nativeObject, focus, scale);
  259. GDIPlus.CheckStatus (status);
  260. }
  261. public void SetSigmaBellShape (float focus)
  262. {
  263. SetSigmaBellShape (focus, 1.0F);
  264. }
  265. public void SetSigmaBellShape (float focus, float scale)
  266. {
  267. if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
  268. throw new ArgumentException ("Invalid parameter passed.");
  269. Status status = GDIPlus.GdipSetLineSigmaBlend (nativeObject, focus, scale);
  270. GDIPlus.CheckStatus (status);
  271. }
  272. public void TranslateTransform (float dx, float dy)
  273. {
  274. TranslateTransform (dx, dy, MatrixOrder.Prepend);
  275. }
  276. public void TranslateTransform (float dx, float dy, MatrixOrder order)
  277. {
  278. Status status = GDIPlus.GdipTranslateLineTransform (nativeObject, dx, dy, order);
  279. GDIPlus.CheckStatus (status);
  280. }
  281. public override object Clone ()
  282. {
  283. IntPtr clonePtr;
  284. Status status = GDIPlus.GdipCloneBrush (nativeObject, out clonePtr);
  285. GDIPlus.CheckStatus (status);
  286. return new LinearGradientBrush (clonePtr);
  287. }
  288. }
  289. }