Bump corefx
[mono.git] / mcs / class / System.Drawing / System.Drawing.Drawing2D / LinearGradientBrush.cs
1 //
2 // System.Drawing.Drawing2D.LinearGradientBrush.cs
3 //
4 // Authors:
5 //   Dennis Hayes (dennish@Raytek.com)
6 //   Ravindra (rkumar@novell.com)
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
31 using System.ComponentModel;
32 using System.Runtime.InteropServices;
33
34 namespace System.Drawing.Drawing2D {
35
36         public sealed class LinearGradientBrush : Brush
37         {
38                 RectangleF rectangle;
39                 
40                 internal LinearGradientBrush (IntPtr native)
41                 {
42                         Status status = GDIPlus.GdipGetLineRect (native, out rectangle);
43                         SetNativeBrush (native);
44                         GDIPlus.CheckStatus (status);
45                 }
46
47                 public LinearGradientBrush (Point point1, Point point2, Color color1, Color color2)
48                 {
49                         IntPtr nativeObject;
50                         Status status = GDIPlus.GdipCreateLineBrushI (ref point1, ref point2, color1.ToArgb (), color2.ToArgb (), WrapMode.Tile, out nativeObject);
51                         GDIPlus.CheckStatus (status);
52                         SetNativeBrush (nativeObject);
53
54                         status = GDIPlus.GdipGetLineRect (nativeObject, out rectangle);
55                         GDIPlus.CheckStatus (status);
56                 }
57
58                 public LinearGradientBrush (PointF point1, PointF point2, Color color1, Color color2)
59                 {
60                         IntPtr nativeObject;
61                         Status status = GDIPlus.GdipCreateLineBrush (ref point1, ref point2, color1.ToArgb (), color2.ToArgb (), WrapMode.Tile, out nativeObject);
62                         GDIPlus.CheckStatus (status);
63                         SetNativeBrush (nativeObject);
64
65                         status = GDIPlus.GdipGetLineRect (nativeObject, out rectangle);
66                         GDIPlus.CheckStatus (status);
67                 }
68
69                 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
70                 {
71                         if (linearGradientMode < LinearGradientMode.Horizontal || linearGradientMode > LinearGradientMode.BackwardDiagonal) {
72                                 throw new InvalidEnumArgumentException (nameof (linearGradientMode), unchecked ((int)linearGradientMode), typeof (LinearGradientMode));
73                         }
74
75                         if (rect.Width == 0 || rect.Height == 0) {
76                                 throw new ArgumentException( string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ()));
77                         }
78
79                         IntPtr nativeObject;
80                         Status status = GDIPlus.GdipCreateLineBrushFromRectI (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
81                         GDIPlus.CheckStatus (status);
82                         SetNativeBrush (nativeObject);
83
84                         rectangle = (RectangleF) rect;
85                 }
86
87                 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle) : this (rect, color1, color2, angle, false)
88                 {
89                 }
90
91                 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
92                 {
93                         if (linearGradientMode < LinearGradientMode.Horizontal || linearGradientMode > LinearGradientMode.BackwardDiagonal) {
94                                 throw new InvalidEnumArgumentException (nameof (linearGradientMode), unchecked ((int)linearGradientMode), typeof (LinearGradientMode));
95                         }
96
97                         if (rect.Width == 0.0 || rect.Height == 0.0) {
98                                 throw new ArgumentException (string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ()));
99                         }
100
101                         IntPtr nativeObject;
102                         Status status = GDIPlus.GdipCreateLineBrushFromRect (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
103                         GDIPlus.CheckStatus (status);
104                         SetNativeBrush (nativeObject);
105
106                         rectangle = rect;
107                 }
108
109                 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle) : this (rect, color1, color2, angle, false)
110                 {
111                 }
112
113                 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle, bool isAngleScaleable)
114                 {
115                         if (rect.Width == 0 || rect.Height == 0) {
116                                 throw new ArgumentException (string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ()));
117                         }
118
119                         IntPtr nativeObject;
120                         Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngleI (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
121                         GDIPlus.CheckStatus (status);
122                         SetNativeBrush (nativeObject);
123
124                         rectangle = (RectangleF) rect;
125                 }
126
127                 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle, bool isAngleScaleable)
128                 {
129                         if (rect.Width == 0 || rect.Height == 0) {
130                                 throw new ArgumentException (string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ()));
131                         }
132
133                         IntPtr nativeObject;
134                         Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngle (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
135                         GDIPlus.CheckStatus (status);
136                         SetNativeBrush (nativeObject);
137
138                         rectangle = rect;
139                 }
140
141                 // Public Properties
142
143                 public Blend Blend {
144                         get {
145                                 int count;
146                                 Status status = GDIPlus.GdipGetLineBlendCount (NativeBrush, out count);
147                                 GDIPlus.CheckStatus (status);
148                                 float [] factors = new float [count];
149                                 float [] positions = new float [count];
150                                 status = GDIPlus.GdipGetLineBlend (NativeBrush, factors, positions, count);
151                                 GDIPlus.CheckStatus (status);
152
153                                 Blend blend = new Blend ();
154                                 blend.Factors = factors;
155                                 blend.Positions = positions;
156
157                                 return blend;
158                         }
159                         set {
160                                 // no null check, MS throws a NullReferenceException here
161                                 int count;
162                                 float [] factors = value.Factors;
163                                 float [] positions = value.Positions;
164                                 count = factors.Length;
165
166                                 if (count == 0 || positions.Length == 0)
167                                         throw new ArgumentException ("Invalid Blend object. It should have at least 2 elements in each of the factors and positions arrays.");
168
169                                 if (count != positions.Length)
170                                         throw new ArgumentException ("Invalid Blend object. It should contain the same number of factors and positions values.");
171
172                                 if (positions [0] != 0.0F)
173                                         throw new ArgumentException ("Invalid Blend object. The positions array must have 0.0 as its first element.");
174
175                                 if (positions [count - 1] != 1.0F)
176                                         throw new ArgumentException ("Invalid Blend object. The positions array must have 1.0 as its last element.");
177
178                                 Status status = GDIPlus.GdipSetLineBlend (NativeBrush, factors, positions, count);
179                                 GDIPlus.CheckStatus (status);
180                         }
181                 }
182
183                 [MonoTODO ("The GammaCorrection value is ignored when using libgdiplus.")]
184                 public bool GammaCorrection {
185                         get {
186                                 bool gammaCorrection;
187                                 Status status = GDIPlus.GdipGetLineGammaCorrection (NativeBrush, out gammaCorrection);
188                                 GDIPlus.CheckStatus (status);
189                                 return gammaCorrection;
190                         }
191                         set {
192                                 Status status = GDIPlus.GdipSetLineGammaCorrection (NativeBrush, value);
193                                 GDIPlus.CheckStatus (status);
194                         }
195                 }
196
197                 public ColorBlend InterpolationColors {
198                         get {
199                                 int count;
200                                 Status status = GDIPlus.GdipGetLinePresetBlendCount (NativeBrush, out count);
201                                 GDIPlus.CheckStatus (status);
202                                 int [] intcolors = new int [count];
203                                 float [] positions = new float [count];
204                                 status = GDIPlus.GdipGetLinePresetBlend (NativeBrush, intcolors, positions, count);
205                                 GDIPlus.CheckStatus (status);
206
207                                 ColorBlend interpolationColors = new ColorBlend ();
208                                 Color [] colors = new Color [count];
209                                 for (int i = 0; i < count; i++)
210                                         colors [i] = Color.FromArgb (intcolors [i]);
211                                 interpolationColors.Colors = colors;
212                                 interpolationColors.Positions = positions;
213
214                                 return interpolationColors;
215                         }
216                         set {
217                                 if (value == null)
218                                         throw new ArgumentException ("InterpolationColors is null");
219                                 int count;
220                                 Color [] colors = value.Colors;
221                                 float [] positions = value.Positions;
222                                 count = colors.Length;
223
224                                 if (count == 0 || positions.Length == 0)
225                                         throw new ArgumentException ("Invalid ColorBlend object. It should have at least 2 elements in each of the colors and positions arrays.");
226
227                                 if (count != positions.Length)
228                                         throw new ArgumentException ("Invalid ColorBlend object. It should contain the same number of positions and color values.");
229
230                                 if (positions [0] != 0.0F)
231                                         throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 0.0 as its first element.");
232
233                                 if (positions [count - 1] != 1.0F)
234                                         throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 1.0 as its last element.");
235
236                                 int [] blend = new int [colors.Length];
237                                 for (int i = 0; i < colors.Length; i++)
238                                         blend [i] = colors [i].ToArgb ();
239
240                                 Status status = GDIPlus.GdipSetLinePresetBlend (NativeBrush, blend, positions, count);
241                                 GDIPlus.CheckStatus (status);
242                         }
243                 }
244
245                 public Color [] LinearColors {
246                         get {
247                                 int [] colors = new int [2];
248                                 Status status = GDIPlus.GdipGetLineColors (NativeBrush, colors);
249                                 GDIPlus.CheckStatus (status);
250                                 Color [] linearColors = new Color [2];
251                                 linearColors [0] = Color.FromArgb (colors [0]);
252                                 linearColors [1] = Color.FromArgb (colors [1]);
253
254                                 return linearColors;
255                         }
256                         set {
257                                 // no null check, MS throws a NullReferenceException here
258                                 Status status = GDIPlus.GdipSetLineColors (NativeBrush, value [0].ToArgb (), value [1].ToArgb ());
259                                 GDIPlus.CheckStatus (status);
260                         }
261                 }
262
263                 public RectangleF Rectangle {
264                         get {
265                                 return rectangle;
266                         }
267                 }
268
269                 public Matrix Transform {
270                         get {
271                                 Matrix matrix = new Matrix ();
272                                 Status status = GDIPlus.GdipGetLineTransform (NativeBrush, matrix.nativeMatrix);
273                                 GDIPlus.CheckStatus (status);
274
275                                 return matrix;
276                         }
277                         set {
278                                 if (value == null)
279                                         throw new ArgumentNullException ("Transform");
280
281                                 Status status = GDIPlus.GdipSetLineTransform (NativeBrush, value.nativeMatrix);
282                                 GDIPlus.CheckStatus (status);
283                         }
284                 }
285
286                 public WrapMode WrapMode {
287                         get {
288                                 WrapMode wrapMode;
289                                 Status status = GDIPlus.GdipGetLineWrapMode (NativeBrush, out wrapMode);
290                                 GDIPlus.CheckStatus (status);
291
292                                 return wrapMode;
293                         }
294                         set {
295                                 // note: Clamp isn't valid (context wise) but it is checked in libgdiplus
296                                 if ((value < WrapMode.Tile) || (value > WrapMode.Clamp))
297                                         throw new InvalidEnumArgumentException ("WrapMode");
298
299                                 Status status = GDIPlus.GdipSetLineWrapMode (NativeBrush, value);
300                                 GDIPlus.CheckStatus (status);
301                         }
302                 }
303
304                 // Public Methods
305
306                 public void MultiplyTransform (Matrix matrix)
307                 {
308                         MultiplyTransform (matrix, MatrixOrder.Prepend);
309                 }
310
311                 public void MultiplyTransform (Matrix matrix, MatrixOrder order)
312                 {
313                         if (matrix == null)
314                                 throw new ArgumentNullException ("matrix");
315
316                         Status status = GDIPlus.GdipMultiplyLineTransform (NativeBrush, matrix.nativeMatrix, order);
317                         GDIPlus.CheckStatus (status);
318                 }
319
320                 public void ResetTransform ()
321                 {
322                         Status status = GDIPlus.GdipResetLineTransform (NativeBrush);
323                         GDIPlus.CheckStatus (status);
324                 }
325
326                 public void RotateTransform (float angle)
327                 {
328                         RotateTransform (angle, MatrixOrder.Prepend);
329                 }
330
331                 public void RotateTransform (float angle, MatrixOrder order)
332                 {
333                         Status status = GDIPlus.GdipRotateLineTransform (NativeBrush, angle, order);
334                         GDIPlus.CheckStatus (status);
335                 }
336
337                 public void ScaleTransform (float sx, float sy)
338                 {
339                         ScaleTransform (sx, sy, MatrixOrder.Prepend);
340                 }
341
342                 public void ScaleTransform (float sx, float sy, MatrixOrder order)
343                 {
344                         Status status = GDIPlus.GdipScaleLineTransform (NativeBrush, sx, sy, order);
345                         GDIPlus.CheckStatus (status);
346                 }
347
348                 public void SetBlendTriangularShape (float focus)
349                 {
350                         SetBlendTriangularShape (focus, 1.0F);
351                 }
352
353                 public void SetBlendTriangularShape (float focus, float scale)
354                 {
355                         if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
356                                 throw new ArgumentException ("Invalid parameter passed.");
357
358                         Status status = GDIPlus.GdipSetLineLinearBlend (NativeBrush, focus, scale);
359                         GDIPlus.CheckStatus (status);
360                 }
361
362                 public void SetSigmaBellShape (float focus)
363                 {
364                         SetSigmaBellShape (focus, 1.0F);
365                 }
366
367                 public void SetSigmaBellShape (float focus, float scale)
368                 {
369                         if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
370                                 throw new ArgumentException ("Invalid parameter passed.");
371
372                         Status status = GDIPlus.GdipSetLineSigmaBlend (NativeBrush, focus, scale);
373                         GDIPlus.CheckStatus (status);
374                 }
375
376                 public void TranslateTransform (float dx, float dy)
377                 {
378                         TranslateTransform (dx, dy, MatrixOrder.Prepend);
379                 }
380
381                 public void TranslateTransform (float dx, float dy, MatrixOrder order)
382                 {
383                         Status status = GDIPlus.GdipTranslateLineTransform (NativeBrush, dx, dy, order);
384                         GDIPlus.CheckStatus (status);
385                 }
386
387                 public override object Clone ()
388                 {
389                         IntPtr clonePtr;
390                         Status status = (Status) GDIPlus.GdipCloneBrush (new HandleRef (this, NativeBrush), out clonePtr);
391                         GDIPlus.CheckStatus (status);
392
393                         return new LinearGradientBrush (clonePtr);
394                 }
395         }
396 }