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