4 using geom = java.awt.geom;
5 using image = java.awt.image;
7 namespace System.Drawing.Drawing2D {
9 /// Summary description for LinearGradientBrush.
11 public sealed class LinearGradientBrush : Brush {
13 bool _gammaCorrection;
14 ColorBlend _interpolationColors;
16 RectangleF _gradientRectangle;
18 // gradient brush data
25 Color _color1, _color2;
30 GradientPaint _nativeObject = null;
31 protected override Paint NativeObject {
33 if ( _nativeObject == null )
34 _nativeObject = new GradientPaint(
36 new java.awt.Color(_color1.R, _color1.G, _color1.B, _color1.A),
38 new java.awt.Color(_color2.R, _color2.G, _color2.B, _color2.A), _cyclic);
46 #region Initialization
48 private void Init(float x1, float y1, Color color1, float x2, float y2, Color color2, bool cyclic) {
61 private void Init(float x1, float y1, Color color1, float x2, float y2, Color color2, float angle) {
62 _gradientRectangle = new RectangleF(x1, y1, x2-x1, y2-y1);
63 PointF [] points = GetMedianeEnclosingRect(x1, y1, x2, y2, angle);
64 Init(points[0].X, points[0].Y, color1, points[1].X, points[1].Y, color2, false);
67 private void Init(float x1, float y1, Color color1, float x2, float y2, Color color2, LinearGradientMode linearGradientMode) {
68 _gradientRectangle = new RectangleF(x1, y1, x2-x1, y2-y1);
71 switch (linearGradientMode) {
72 case LinearGradientMode.Horizontal :
73 Init(x1, y1, color1, x2, y1, color2, false);
76 case LinearGradientMode.Vertical :
77 Init(x1, y1, color1, x1, y2, color2, false);
80 case LinearGradientMode.BackwardDiagonal :
81 points = GetMedianeEnclosingRect(x1, y1, x2, y2, false);
82 Init(points[0].X, points[0].Y, color2, points[1].X, points[1].Y, color1, false);
85 case LinearGradientMode.ForwardDiagonal :
86 points = GetMedianeEnclosingRect(x1, y1, x2, y2, true);
87 Init(points[0].X, points[0].Y, color1, points[1].X, points[1].Y, color2, false);
91 throw new ArgumentException("LinearGradientMode");
99 private LinearGradientBrush (float x1, float y1, Color color1, float x2, float y2, Color color2, bool cyclic) {
100 Init(x1, y1, color1, x2, y2, color2, cyclic);
103 internal LinearGradientBrush (float x1, float y1, Color color1, float x2, float y2, Color color2, LinearGradientMode mode) {
104 Init(x1, y1, color1, x2, y2, color2, mode);
106 internal LinearGradientBrush (float x1, float y1, Color color1, float x2, float y2, Color color2) {
107 Init(x1, y2, color1, x1, y2, color2, false);
109 public LinearGradientBrush (Point point1, Point point2, Color color1, Color color2) {
110 _gradientRectangle = new RectangleF(point1.X, point1.Y, point2.X - point1.X, point2.Y - point2.Y);
111 Init(point1.X, point1.Y, color1, point2.X, point2.Y, color2, false);
113 public LinearGradientBrush (PointF point1, PointF point2, Color color1, Color color2) {
114 _gradientRectangle = new RectangleF(point1.X, point1.Y, point2.X - point1.X, point2.Y - point2.Y);
115 Init(point1.X, point1.Y, color1, point2.X, point2.Y, color2, false);
117 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, LinearGradientMode linearGradientMode) {
118 Init(rect.X, rect.Y, color1, rect.X + rect.Width, rect.Y + rect.Height, color2, linearGradientMode);
120 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, LinearGradientMode linearGradientMode) {
121 Init(rect.X, rect.Y, color1, rect.X + rect.Width, rect.Y + rect.Height, color2, linearGradientMode);
123 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle) {
124 Init(rect.X, rect.Y, color1, rect.X + rect.Width, rect.Y + rect.Height, color2, angle);
126 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle) {
127 Init(rect.X, rect.Y, color1, rect.X + rect.Width, rect.Y + rect.Height, color2, angle);
129 public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle, bool isAngleScaleable):
130 this(rect, color1, color2, angle) {
132 public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle, bool isAngleScaleable):
133 this(rect, color1, color2, angle) {
137 #region GetMedianeEnclosingRect
139 internal PointF [] GetMedianeEnclosingRect(float x1, float y1, float x2, float y2, float rotateAngle) {
140 float width = x2 - x1;
141 float height = y2 - y1;
142 PointF rectCenter = new PointF(x1 + width/2, y1 + height/2);
143 float gradLen = width * Math.Abs((float)Math.Cos(rotateAngle * Math.PI / 180)) +
144 height * Math.Abs((float)Math.Sin(rotateAngle * Math.PI / 180));
146 PointF [] points = new PointF []{ new PointF(rectCenter.X - gradLen/2, rectCenter.Y),
147 new PointF(rectCenter.X + gradLen/2, rectCenter.Y) };
149 Matrix mx = new Matrix();
150 mx.RotateAt((float)rotateAngle, rectCenter);
151 mx.TransformPoints(points);
154 internal PointF [] GetMedianeEnclosingRect(float x1, float y1, float x2, float y2, bool forwardDiagonal) {
155 float width = x2 - x1;
156 float height = y2 - y1;
157 PointF rectCenter = new PointF(x1 + width/2, y1 + height/2);
158 float rotateAngle = (float)Math.Atan2(width, height);
159 float gradLen = width * (float)Math.Cos(rotateAngle);
161 if (!forwardDiagonal)
162 rotateAngle = -rotateAngle;
164 PointF [] points = new PointF []{ new PointF(rectCenter.X - gradLen, rectCenter.Y),
165 new PointF(rectCenter.X + gradLen, rectCenter.Y) };
167 Matrix mx = new Matrix();
168 mx.RotateAt((float)rotateAngle * (float)(180/Math.PI), rectCenter);
169 mx.TransformPoints(points);
175 #region Public Properties
177 // FALLBACK: no functionality implemented for this property
188 // FALLBACK: no functionality implemented for this property
190 public bool GammaCorrection {
192 return _gammaCorrection;
195 _gammaCorrection = value;
199 // FALLBACK: functionality of two color gradient is implemented
201 public ColorBlend InterpolationColors {
203 return _interpolationColors;
207 throw new ArgumentNullException("ColorBlend");
209 if ( (value.Colors == null) || (value.Colors.Length == 0) )
210 throw new ArgumentException("ColorBlend");
212 _interpolationColors = value;
214 _color1 = value.Colors[0];
215 _color2 = value.Colors[value.Colors.Length - 1];
216 _nativeObject = null;
220 public Color [] LinearColors {
222 Color [] cl = new Color[2];
229 throw new ArgumentNullException("colors");
233 _nativeObject = null;
237 public RectangleF Rectangle {
239 return _gradientRectangle;
243 public Matrix Transform {
244 get { return BrushTransform; }
245 set { BrushTransform = value; }
248 // FALLBACK: not functionality implemented for this property
250 public WrapMode WrapMode {
260 #region Public Methods
262 public void MultiplyTransform (Matrix matrix) {
263 BrushMultiplyTransform(matrix, MatrixOrder.Prepend);
266 public void MultiplyTransform (Matrix matrix, MatrixOrder order) {
267 BrushMultiplyTransform(matrix, order);
270 public void ResetTransform () {
271 BrushResetTransform();
274 public void RotateTransform (float angle) {
275 BrushRotateTransform(angle, MatrixOrder.Prepend);
278 public void RotateTransform (float angle, MatrixOrder order) {
279 BrushRotateTransform(angle, order);
282 public void ScaleTransform (float sx, float sy) {
283 BrushScaleTransform(sx, sy, MatrixOrder.Prepend);
286 public void ScaleTransform (float sx, float sy, MatrixOrder order) {
287 BrushScaleTransform(sx, sy, order);
290 public void SetBlendTriangularShape (float focus) {
291 SetBlendTriangularShape (focus, 1.0F);
294 public void SetBlendTriangularShape (float focus, float scale) {
295 _x2 = (_x1 + _x2) / 2;
296 _y2 = (_y1 + _y2) / 2;
298 _nativeObject = null;
301 public void SetSigmaBellShape (float focus) {
302 SetSigmaBellShape (focus, 1.0F);
306 public void SetSigmaBellShape (float focus, float scale) {
307 // FALLBACK: Triangle shape used
308 SetBlendTriangularShape (focus, scale);
311 public void TranslateTransform (float dx, float dy) {
312 BrushTranslateTransform (dx, dy);
315 public void TranslateTransform (float dx, float dy, MatrixOrder order) {
316 BrushTranslateTransform(dx, dy, order);
319 public override object Clone () {
320 LinearGradientBrush copy = (LinearGradientBrush)InternalClone();
322 if (copy._nativeObject != null)
323 copy._nativeObject = null;
325 if (_interpolationColors != null) {
326 copy._interpolationColors = new ColorBlend();
327 if (_interpolationColors.Colors != null)
328 copy._interpolationColors.Colors = (Color[])_interpolationColors.Colors.Clone();
329 if (_interpolationColors.Positions != null)
330 copy._interpolationColors.Positions = (float[])_interpolationColors.Positions.Clone();
333 if (_blend != null) {
334 copy._blend = new Blend();
335 if (_blend.Factors != null)
336 copy._blend.Factors = (float[])_blend.Factors.Clone();
337 if (_blend.Positions != null)
338 copy._blend.Positions = (float[])_blend.Positions.Clone();
341 copy.LinearColors = (Color[])LinearColors.Clone();