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