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)
70 if (linearGradientMode < LinearGradientMode.Horizontal || linearGradientMode > LinearGradientMode.BackwardDiagonal) {
71 throw new InvalidEnumArgumentException (nameof (linearGradientMode), unchecked ((int)linearGradientMode), typeof (LinearGradientMode));
74 if (rect.Width == 0 || rect.Height == 0) {
75 throw new ArgumentException( string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ()));
79 Status status = GDIPlus.GdipCreateLineBrushFromRectI (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
80 GDIPlus.CheckStatus (status);
81 SetNativeBrush (nativeObject);
83 rectangle = (RectangleF) rect;
86 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle) : this (rect, color1, color2, angle, false)
90 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
92 if (linearGradientMode < LinearGradientMode.Horizontal || linearGradientMode > LinearGradientMode.BackwardDiagonal) {
93 throw new InvalidEnumArgumentException (nameof (linearGradientMode), unchecked ((int)linearGradientMode), typeof (LinearGradientMode));
96 if (rect.Width == 0.0 || rect.Height == 0.0) {
97 throw new ArgumentException (string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ()));
101 Status status = GDIPlus.GdipCreateLineBrushFromRect (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
102 GDIPlus.CheckStatus (status);
103 SetNativeBrush (nativeObject);
108 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle) : this (rect, color1, color2, angle, false)
112 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle, bool isAngleScaleable)
114 if (rect.Width == 0 || rect.Height == 0) {
115 throw new ArgumentException (string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ()));
119 Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngleI (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
120 GDIPlus.CheckStatus (status);
121 SetNativeBrush (nativeObject);
123 rectangle = (RectangleF) rect;
126 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle, bool isAngleScaleable)
128 if (rect.Width == 0 || rect.Height == 0) {
129 throw new ArgumentException (string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ()));
133 Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngle (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
134 GDIPlus.CheckStatus (status);
135 SetNativeBrush (nativeObject);
145 Status status = GDIPlus.GdipGetLineBlendCount (NativeBrush, out count);
146 GDIPlus.CheckStatus (status);
147 float [] factors = new float [count];
148 float [] positions = new float [count];
149 status = GDIPlus.GdipGetLineBlend (NativeBrush, factors, positions, count);
150 GDIPlus.CheckStatus (status);
152 Blend blend = new Blend ();
153 blend.Factors = factors;
154 blend.Positions = positions;
159 // no null check, MS throws a NullReferenceException here
161 float [] factors = value.Factors;
162 float [] positions = value.Positions;
163 count = factors.Length;
165 if (count == 0 || positions.Length == 0)
166 throw new ArgumentException ("Invalid Blend object. It should have at least 2 elements in each of the factors and positions arrays.");
168 if (count != positions.Length)
169 throw new ArgumentException ("Invalid Blend object. It should contain the same number of factors and positions values.");
171 if (positions [0] != 0.0F)
172 throw new ArgumentException ("Invalid Blend object. The positions array must have 0.0 as its first element.");
174 if (positions [count - 1] != 1.0F)
175 throw new ArgumentException ("Invalid Blend object. The positions array must have 1.0 as its last element.");
177 Status status = GDIPlus.GdipSetLineBlend (NativeBrush, factors, positions, count);
178 GDIPlus.CheckStatus (status);
182 [MonoTODO ("The GammaCorrection value is ignored when using libgdiplus.")]
183 public bool GammaCorrection {
185 bool gammaCorrection;
186 Status status = GDIPlus.GdipGetLineGammaCorrection (NativeBrush, out gammaCorrection);
187 GDIPlus.CheckStatus (status);
188 return gammaCorrection;
191 Status status = GDIPlus.GdipSetLineGammaCorrection (NativeBrush, value);
192 GDIPlus.CheckStatus (status);
196 public ColorBlend InterpolationColors {
199 Status status = GDIPlus.GdipGetLinePresetBlendCount (NativeBrush, out count);
200 GDIPlus.CheckStatus (status);
201 int [] intcolors = new int [count];
202 float [] positions = new float [count];
203 status = GDIPlus.GdipGetLinePresetBlend (NativeBrush, intcolors, positions, count);
204 GDIPlus.CheckStatus (status);
206 ColorBlend interpolationColors = new ColorBlend ();
207 Color [] colors = new Color [count];
208 for (int i = 0; i < count; i++)
209 colors [i] = Color.FromArgb (intcolors [i]);
210 interpolationColors.Colors = colors;
211 interpolationColors.Positions = positions;
213 return interpolationColors;
217 throw new ArgumentException ("InterpolationColors is null");
219 Color [] colors = value.Colors;
220 float [] positions = value.Positions;
221 count = colors.Length;
223 if (count == 0 || positions.Length == 0)
224 throw new ArgumentException ("Invalid ColorBlend object. It should have at least 2 elements in each of the colors and positions arrays.");
226 if (count != positions.Length)
227 throw new ArgumentException ("Invalid ColorBlend object. It should contain the same number of positions and color values.");
229 if (positions [0] != 0.0F)
230 throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 0.0 as its first element.");
232 if (positions [count - 1] != 1.0F)
233 throw new ArgumentException ("Invalid ColorBlend object. The positions array must have 1.0 as its last element.");
235 int [] blend = new int [colors.Length];
236 for (int i = 0; i < colors.Length; i++)
237 blend [i] = colors [i].ToArgb ();
239 Status status = GDIPlus.GdipSetLinePresetBlend (NativeBrush, blend, positions, count);
240 GDIPlus.CheckStatus (status);
244 public Color [] LinearColors {
246 int [] colors = new int [2];
247 Status status = GDIPlus.GdipGetLineColors (NativeBrush, colors);
248 GDIPlus.CheckStatus (status);
249 Color [] linearColors = new Color [2];
250 linearColors [0] = Color.FromArgb (colors [0]);
251 linearColors [1] = Color.FromArgb (colors [1]);
256 // no null check, MS throws a NullReferenceException here
257 Status status = GDIPlus.GdipSetLineColors (NativeBrush, value [0].ToArgb (), value [1].ToArgb ());
258 GDIPlus.CheckStatus (status);
262 public RectangleF Rectangle {
268 public Matrix Transform {
270 Matrix matrix = new Matrix ();
271 Status status = GDIPlus.GdipGetLineTransform (NativeBrush, matrix.nativeMatrix);
272 GDIPlus.CheckStatus (status);
278 throw new ArgumentNullException ("Transform");
280 Status status = GDIPlus.GdipSetLineTransform (NativeBrush, value.nativeMatrix);
281 GDIPlus.CheckStatus (status);
285 public WrapMode WrapMode {
288 Status status = GDIPlus.GdipGetLineWrapMode (NativeBrush, out wrapMode);
289 GDIPlus.CheckStatus (status);
294 // note: Clamp isn't valid (context wise) but it is checked in libgdiplus
295 if ((value < WrapMode.Tile) || (value > WrapMode.Clamp))
296 throw new InvalidEnumArgumentException ("WrapMode");
298 Status status = GDIPlus.GdipSetLineWrapMode (NativeBrush, value);
299 GDIPlus.CheckStatus (status);
305 public void MultiplyTransform (Matrix matrix)
307 MultiplyTransform (matrix, MatrixOrder.Prepend);
310 public void MultiplyTransform (Matrix matrix, MatrixOrder order)
313 throw new ArgumentNullException ("matrix");
315 Status status = GDIPlus.GdipMultiplyLineTransform (NativeBrush, matrix.nativeMatrix, order);
316 GDIPlus.CheckStatus (status);
319 public void ResetTransform ()
321 Status status = GDIPlus.GdipResetLineTransform (NativeBrush);
322 GDIPlus.CheckStatus (status);
325 public void RotateTransform (float angle)
327 RotateTransform (angle, MatrixOrder.Prepend);
330 public void RotateTransform (float angle, MatrixOrder order)
332 Status status = GDIPlus.GdipRotateLineTransform (NativeBrush, angle, order);
333 GDIPlus.CheckStatus (status);
336 public void ScaleTransform (float sx, float sy)
338 ScaleTransform (sx, sy, MatrixOrder.Prepend);
341 public void ScaleTransform (float sx, float sy, MatrixOrder order)
343 Status status = GDIPlus.GdipScaleLineTransform (NativeBrush, sx, sy, order);
344 GDIPlus.CheckStatus (status);
347 public void SetBlendTriangularShape (float focus)
349 SetBlendTriangularShape (focus, 1.0F);
352 public void SetBlendTriangularShape (float focus, float scale)
354 if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
355 throw new ArgumentException ("Invalid parameter passed.");
357 Status status = GDIPlus.GdipSetLineLinearBlend (NativeBrush, focus, scale);
358 GDIPlus.CheckStatus (status);
361 public void SetSigmaBellShape (float focus)
363 SetSigmaBellShape (focus, 1.0F);
366 public void SetSigmaBellShape (float focus, float scale)
368 if (focus < 0 || focus > 1 || scale < 0 || scale > 1)
369 throw new ArgumentException ("Invalid parameter passed.");
371 Status status = GDIPlus.GdipSetLineSigmaBlend (NativeBrush, focus, scale);
372 GDIPlus.CheckStatus (status);
375 public void TranslateTransform (float dx, float dy)
377 TranslateTransform (dx, dy, MatrixOrder.Prepend);
380 public void TranslateTransform (float dx, float dy, MatrixOrder order)
382 Status status = GDIPlus.GdipTranslateLineTransform (NativeBrush, dx, dy, order);
383 GDIPlus.CheckStatus (status);
386 public override object Clone ()
389 Status status = GDIPlus.GdipCloneBrush (NativeBrush, out clonePtr);
390 GDIPlus.CheckStatus (status);
392 return new LinearGradientBrush (clonePtr);