svn path=/branches/mono-1-1-9/mcs/; revision=51207
[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 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Drawing;
33
34 namespace System.Drawing.Drawing2D
35 {
36         /// <summary>
37         /// Summary description for LinearGradientBrush.
38         /// </summary>
39         public sealed class LinearGradientBrush : Brush
40         {
41                 RectangleF rectangle;
42                 
43                 internal LinearGradientBrush (IntPtr native) : base (native)
44                 {
45                 }
46
47                 public LinearGradientBrush (Point point1, Point point2, Color color1, Color color2)
48                 {
49                         Status status = GDIPlus.GdipCreateLineBrushI (ref point1, ref point2, color1.ToArgb (), color2.ToArgb (), WrapMode.Tile, out nativeObject);
50                         GDIPlus.CheckStatus (status);
51
52                         Rectangle rect;
53                         status = GDIPlus.GdipGetLineRectI (nativeObject, out rect);
54                         GDIPlus.CheckStatus (status);
55                         rectangle = (RectangleF) rect;
56                 }
57
58                 public LinearGradientBrush (PointF point1, PointF point2, Color color1, Color color2)
59                 {
60                         Status status = GDIPlus.GdipCreateLineBrush (ref point1, ref point2, color1.ToArgb (), color2.ToArgb (), WrapMode.Tile, out nativeObject);
61                         GDIPlus.CheckStatus (status);
62
63                         status = GDIPlus.GdipGetLineRect (nativeObject, out rectangle);
64                         GDIPlus.CheckStatus (status);
65                 }
66
67                 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
68                 {
69                         Status status = GDIPlus.GdipCreateLineBrushFromRectI (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
70                         GDIPlus.CheckStatus (status);
71
72                         rectangle = (RectangleF) rect;
73                 }
74
75                 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle) : this (rect, color1, color2, angle, false)
76                 {
77                 }
78
79                 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
80                 {
81                         Status status = GDIPlus.GdipCreateLineBrushFromRect (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
82                         GDIPlus.CheckStatus (status);
83
84                         rectangle = rect;
85                 }
86
87                 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle) : this (rect, color1, color2, angle, false)
88                 {
89                 }
90
91                 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle, bool isAngleScaleable)
92                 {
93                         Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngleI (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
94                         GDIPlus.CheckStatus (status);
95
96                         rectangle = (RectangleF) rect;
97                 }
98
99                 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle, bool isAngleScaleable)
100                 {
101                         Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngle (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
102                         GDIPlus.CheckStatus (status);
103
104                         rectangle = rect;
105                 }
106
107                 // Public Properties
108
109                 public Blend Blend {
110                         get {
111                                 int count;
112                                 Status status = GDIPlus.GdipGetLineBlendCount (nativeObject, out count);
113                                 GDIPlus.CheckStatus (status);
114                                 float [] factors = new float [count];
115                                 float [] positions = new float [count];
116                                 status = GDIPlus.GdipGetLineBlend (nativeObject, factors, positions, count);
117                                 GDIPlus.CheckStatus (status);
118
119                                 Blend blend = new Blend ();
120                                 blend.Factors = factors;
121                                 blend.Positions = positions;
122
123                                 return blend;
124                         }
125                         set {
126                                 int count;
127                                 float [] factors = value.Factors;
128                                 float [] positions = value.Positions;
129                                 count = factors.Length;
130
131                                 if (count == 0 || positions.Length == 0)
132                                         throw new ArgumentException ("Invalid Blend object. It should have at least 2 elements in each of the factors and positions arrays.");
133
134                                 if (count != positions.Length)
135                                         throw new ArgumentException ("Invalid Blend object. It should contain the same number of factors and positions values.");
136
137                                 if (positions [0] != 0.0F)
138                                         throw new ArgumentException ("Invalid Blend object. The positions array must have 0.0 as its first element.");
139
140                                 if (positions [count - 1] != 1.0F)
141                                         throw new ArgumentException ("Invalid Blend object. The positions array must have 1.0 as its last element.");
142
143                                 Status status = GDIPlus.GdipSetLineBlend (nativeObject, factors, positions, count);
144                                 GDIPlus.CheckStatus (status);
145                         }
146                 }
147
148                 public bool GammaCorrection {
149                         get {
150                                 bool gammaCorrection;
151                                 Status status = GDIPlus.GdipGetLineGammaCorrection (nativeObject, out gammaCorrection);
152                                 GDIPlus.CheckStatus (status);
153                                 return gammaCorrection;
154                         }
155                         set {
156                                 Status status = GDIPlus.GdipSetLineGammaCorrection (nativeObject, value);
157                                 GDIPlus.CheckStatus (status);
158                         }
159                 }
160
161                 public ColorBlend InterpolationColors {
162                         get {
163                                 int count;
164                                 Status status = GDIPlus.GdipGetLinePresetBlendCount (nativeObject, out count);
165                                 GDIPlus.CheckStatus (status);
166                                 int [] intcolors = new int [count];
167                                 float [] positions = new float [count];
168                                 status = GDIPlus.GdipGetLinePresetBlend (nativeObject, intcolors, positions, count);
169                                 GDIPlus.CheckStatus (status);
170
171                                 ColorBlend interpolationColors = new ColorBlend ();
172                                 Color [] colors = new Color [count];
173                                 for (int i = 0; i < count; i++)
174                                         colors [i] = Color.FromArgb (intcolors [i]);
175                                 interpolationColors.Colors = colors;
176                                 interpolationColors.Positions = positions;
177
178                                 return interpolationColors;
179                         }
180                         set {
181                                 int count;
182                                 Color [] colors = value.Colors;
183                                 float [] positions = value.Positions;
184                                 count = colors.Length;
185
186                                 if (count == 0 || positions.Length == 0)
187                                         throw new ArgumentException ("Invalid ColorBlend object. It should have at least 2 elements in each of the colors and positions arrays.");
188
189                                 if (count != positions.Length)
190                                         throw new ArgumentException ("Invalid ColorBlend object. It should contain the same number of positions and color values.");
191
192                                 if (positions [0] != 0.0F)
193                                         throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 0.0 as its first element.");
194
195                                 if (positions [count - 1] != 1.0F)
196                                         throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 1.0 as its last element.");
197
198                                 int [] blend = new int [colors.Length];
199                                 for (int i = 0; i < colors.Length; i++)
200                                         blend [i] = colors [i].ToArgb ();
201
202                                 Status status = GDIPlus.GdipSetLinePresetBlend (nativeObject, blend, positions, count);
203                                 GDIPlus.CheckStatus (status);
204                         }
205                 }
206
207                 public Color [] LinearColors {
208                         get {
209                                 int [] colors = new int [2];
210                                 Status status = GDIPlus.GdipGetLineColors (nativeObject, colors);
211                                 GDIPlus.CheckStatus (status);
212                                 Color [] linearColors = new Color [2];
213                                 linearColors [0] = Color.FromArgb (colors [0]);
214                                 linearColors [1] = Color.FromArgb (colors [1]);
215
216                                 return linearColors;
217                         }
218                         set {
219                                 Status status = GDIPlus.GdipSetLineColors (nativeObject, value [0].ToArgb (), value [1].ToArgb ());
220                                 GDIPlus.CheckStatus (status);
221                         }
222                 }
223
224                 public RectangleF Rectangle {
225                         get {
226                                 return rectangle;
227                         }
228                 }
229
230                 public Matrix Transform {
231                         get {
232                                 Matrix matrix = new Matrix ();
233                                 Status status = GDIPlus.GdipGetLineTransform (nativeObject, matrix.nativeMatrix);
234                                 GDIPlus.CheckStatus (status);
235
236                                 return matrix;
237                         }
238                         set {
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                                 Status status = GDIPlus.GdipSetLineWrapMode (nativeObject, value);
254                                 GDIPlus.CheckStatus (status);
255                         }
256                 }
257
258                 // Public Methods
259
260                 public void MultiplyTransform (Matrix matrix)
261                 {
262                         MultiplyTransform (matrix, MatrixOrder.Prepend);
263                 }
264
265                 public void MultiplyTransform (Matrix matrix, MatrixOrder order)
266                 {
267                         Status status = GDIPlus.GdipMultiplyLineTransform (nativeObject, matrix.nativeMatrix, order);
268                         GDIPlus.CheckStatus (status);
269                 }
270
271                 public void ResetTransform ()
272                 {
273                         Status status = GDIPlus.GdipResetLineTransform (nativeObject);
274                         GDIPlus.CheckStatus (status);
275                 }
276
277                 public void RotateTransform (float angle)
278                 {
279                         RotateTransform (angle, MatrixOrder.Prepend);
280                 }
281
282                 public void RotateTransform (float angle, MatrixOrder order)
283                 {
284                         Status status = GDIPlus.GdipRotateLineTransform (nativeObject, angle, order);
285                         GDIPlus.CheckStatus (status);
286                 }
287
288                 public void ScaleTransform (float sx, float sy)
289                 {
290                         ScaleTransform (sx, sy, MatrixOrder.Prepend);
291                 }
292
293                 public void ScaleTransform (float sx, float sy, MatrixOrder order)
294                 {
295                         Status status = GDIPlus.GdipScaleLineTransform (nativeObject, sx, sy, order);
296                         GDIPlus.CheckStatus (status);
297                 }
298
299                 public void SetBlendTriangularShape (float focus)
300                 {
301                         SetBlendTriangularShape (focus, 1.0F);
302                 }
303
304                 public void SetBlendTriangularShape (float focus, float scale)
305                 {
306                         if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
307                                 throw new ArgumentException ("Invalid parameter passed.");
308
309                         Status status = GDIPlus.GdipSetLineLinearBlend (nativeObject, focus, scale);
310                         GDIPlus.CheckStatus (status);
311                 }
312
313                 public void SetSigmaBellShape (float focus)
314                 {
315                         SetSigmaBellShape (focus, 1.0F);
316                 }
317
318                 public void SetSigmaBellShape (float focus, float scale)
319                 {
320                         if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
321                                 throw new ArgumentException ("Invalid parameter passed.");
322
323                         Status status = GDIPlus.GdipSetLineSigmaBlend (nativeObject, focus, scale);
324                         GDIPlus.CheckStatus (status);
325                 }
326
327                 public void TranslateTransform (float dx, float dy)
328                 {
329                         TranslateTransform (dx, dy, MatrixOrder.Prepend);
330                 }
331
332                 public void TranslateTransform (float dx, float dy, MatrixOrder order)
333                 {
334                         Status status = GDIPlus.GdipTranslateLineTransform (nativeObject, dx, dy, order);
335                         GDIPlus.CheckStatus (status);
336                 }
337
338                 public override object Clone ()
339                 {
340                         IntPtr clonePtr;
341                         Status status = GDIPlus.GdipCloneBrush (nativeObject, out clonePtr);
342                         GDIPlus.CheckStatus (status);
343
344                         LinearGradientBrush clone = new LinearGradientBrush (clonePtr);
345                         return clone;
346                 }
347         }
348 }