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