2 // System.Drawing.Drawing2D.LinearGradientBrush.cs
5 // Dennis Hayes (dennish@Raytek.com)
6 // Ravindra (rkumar@novell.com)
8 // Copyright (C) 2002/3 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.ComponentModel;
33 namespace System.Drawing.Drawing2D {
35 public sealed class LinearGradientBrush : Brush
39 internal LinearGradientBrush (IntPtr native)
41 Status status = GDIPlus.GdipGetLineRect (native, out rectangle);
42 SetNativeBrush (native);
43 GDIPlus.CheckStatus (status);
46 public LinearGradientBrush (Point point1, Point point2, Color color1, Color color2)
49 Status status = GDIPlus.GdipCreateLineBrushI (ref point1, ref point2, color1.ToArgb (), color2.ToArgb (), WrapMode.Tile, out nativeObject);
50 GDIPlus.CheckStatus (status);
51 SetNativeBrush (nativeObject);
53 status = GDIPlus.GdipGetLineRect (nativeObject, out rectangle);
54 GDIPlus.CheckStatus (status);
57 public LinearGradientBrush (PointF point1, PointF point2, Color color1, Color color2)
60 Status status = GDIPlus.GdipCreateLineBrush (ref point1, ref point2, color1.ToArgb (), color2.ToArgb (), WrapMode.Tile, out nativeObject);
61 GDIPlus.CheckStatus (status);
62 SetNativeBrush (nativeObject);
64 status = GDIPlus.GdipGetLineRect (nativeObject, out rectangle);
65 GDIPlus.CheckStatus (status);
68 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
71 Status status = GDIPlus.GdipCreateLineBrushFromRectI (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
72 GDIPlus.CheckStatus (status);
73 SetNativeBrush (nativeObject);
75 rectangle = (RectangleF) rect;
78 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle) : this (rect, color1, color2, angle, false)
82 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
85 Status status = GDIPlus.GdipCreateLineBrushFromRect (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
86 GDIPlus.CheckStatus (status);
87 SetNativeBrush (nativeObject);
92 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle) : this (rect, color1, color2, angle, false)
96 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle, bool isAngleScaleable)
99 Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngleI (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
100 GDIPlus.CheckStatus (status);
101 SetNativeBrush (nativeObject);
103 rectangle = (RectangleF) rect;
106 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle, bool isAngleScaleable)
109 Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngle (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
110 GDIPlus.CheckStatus (status);
111 SetNativeBrush (nativeObject);
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);
128 Blend blend = new Blend ();
129 blend.Factors = factors;
130 blend.Positions = positions;
135 // no null check, MS throws a NullReferenceException here
137 float [] factors = value.Factors;
138 float [] positions = value.Positions;
139 count = factors.Length;
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.");
144 if (count != positions.Length)
145 throw new ArgumentException ("Invalid Blend object. It should contain the same number of factors and positions values.");
147 if (positions [0] != 0.0F)
148 throw new ArgumentException ("Invalid Blend object. The positions array must have 0.0 as its first element.");
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.");
153 Status status = GDIPlus.GdipSetLineBlend (NativeBrush, factors, positions, count);
154 GDIPlus.CheckStatus (status);
158 [MonoTODO ("The GammaCorrection value is ignored when using libgdiplus.")]
159 public bool GammaCorrection {
161 bool gammaCorrection;
162 Status status = GDIPlus.GdipGetLineGammaCorrection (NativeBrush, out gammaCorrection);
163 GDIPlus.CheckStatus (status);
164 return gammaCorrection;
167 Status status = GDIPlus.GdipSetLineGammaCorrection (NativeBrush, value);
168 GDIPlus.CheckStatus (status);
172 public ColorBlend InterpolationColors {
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);
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;
189 return interpolationColors;
193 throw new ArgumentException ("InterpolationColors is null");
195 Color [] colors = value.Colors;
196 float [] positions = value.Positions;
197 count = colors.Length;
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.");
202 if (count != positions.Length)
203 throw new ArgumentException ("Invalid ColorBlend object. It should contain the same number of positions and color values.");
205 if (positions [0] != 0.0F)
206 throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 0.0 as its first element.");
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.");
211 int [] blend = new int [colors.Length];
212 for (int i = 0; i < colors.Length; i++)
213 blend [i] = colors [i].ToArgb ();
215 Status status = GDIPlus.GdipSetLinePresetBlend (NativeBrush, blend, positions, count);
216 GDIPlus.CheckStatus (status);
220 public Color [] LinearColors {
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]);
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);
238 public RectangleF Rectangle {
244 public Matrix Transform {
246 Matrix matrix = new Matrix ();
247 Status status = GDIPlus.GdipGetLineTransform (NativeBrush, matrix.nativeMatrix);
248 GDIPlus.CheckStatus (status);
254 throw new ArgumentNullException ("Transform");
256 Status status = GDIPlus.GdipSetLineTransform (NativeBrush, value.nativeMatrix);
257 GDIPlus.CheckStatus (status);
261 public WrapMode WrapMode {
264 Status status = GDIPlus.GdipGetLineWrapMode (NativeBrush, out wrapMode);
265 GDIPlus.CheckStatus (status);
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");
274 Status status = GDIPlus.GdipSetLineWrapMode (NativeBrush, value);
275 GDIPlus.CheckStatus (status);
281 public void MultiplyTransform (Matrix matrix)
283 MultiplyTransform (matrix, MatrixOrder.Prepend);
286 public void MultiplyTransform (Matrix matrix, MatrixOrder order)
289 throw new ArgumentNullException ("matrix");
291 Status status = GDIPlus.GdipMultiplyLineTransform (NativeBrush, matrix.nativeMatrix, order);
292 GDIPlus.CheckStatus (status);
295 public void ResetTransform ()
297 Status status = GDIPlus.GdipResetLineTransform (NativeBrush);
298 GDIPlus.CheckStatus (status);
301 public void RotateTransform (float angle)
303 RotateTransform (angle, MatrixOrder.Prepend);
306 public void RotateTransform (float angle, MatrixOrder order)
308 Status status = GDIPlus.GdipRotateLineTransform (NativeBrush, angle, order);
309 GDIPlus.CheckStatus (status);
312 public void ScaleTransform (float sx, float sy)
314 ScaleTransform (sx, sy, MatrixOrder.Prepend);
317 public void ScaleTransform (float sx, float sy, MatrixOrder order)
319 Status status = GDIPlus.GdipScaleLineTransform (NativeBrush, sx, sy, order);
320 GDIPlus.CheckStatus (status);
323 public void SetBlendTriangularShape (float focus)
325 SetBlendTriangularShape (focus, 1.0F);
328 public void SetBlendTriangularShape (float focus, float scale)
330 if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
331 throw new ArgumentException ("Invalid parameter passed.");
333 Status status = GDIPlus.GdipSetLineLinearBlend (NativeBrush, focus, scale);
334 GDIPlus.CheckStatus (status);
337 public void SetSigmaBellShape (float focus)
339 SetSigmaBellShape (focus, 1.0F);
342 public void SetSigmaBellShape (float focus, float scale)
344 if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
345 throw new ArgumentException ("Invalid parameter passed.");
347 Status status = GDIPlus.GdipSetLineSigmaBlend (NativeBrush, focus, scale);
348 GDIPlus.CheckStatus (status);
351 public void TranslateTransform (float dx, float dy)
353 TranslateTransform (dx, dy, MatrixOrder.Prepend);
356 public void TranslateTransform (float dx, float dy, MatrixOrder order)
358 Status status = GDIPlus.GdipTranslateLineTransform (NativeBrush, dx, dy, order);
359 GDIPlus.CheckStatus (status);
362 public override object Clone ()
365 Status status = GDIPlus.GdipCloneBrush (NativeBrush, out clonePtr);
366 GDIPlus.CheckStatus (status);
368 return new LinearGradientBrush (clonePtr);