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