File: LinearGradientBrush.jvm.cs

package info (click to toggle)
mono 2.6.7-5.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 327,344 kB
  • ctags: 413,649
  • sloc: cs: 2,471,883; xml: 1,768,594; ansic: 350,665; sh: 13,644; makefile: 8,640; perl: 1,784; asm: 717; cpp: 209; python: 146; sql: 81; sed: 16
file content (346 lines) | stat: -rw-r--r-- 10,131 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using System;
using java.awt;
using awt = java.awt;
using geom = java.awt.geom;
using image = java.awt.image;

namespace System.Drawing.Drawing2D {
	/// <summary>
	/// Summary description for LinearGradientBrush.
	/// </summary>
	public sealed class LinearGradientBrush : Brush {
		Blend _blend;
		bool _gammaCorrection;
		ColorBlend _interpolationColors;
		WrapMode _wrapmode;
		RectangleF _gradientRectangle;

		// gradient brush data
		float _x1 = 0;
		float _y1 = 0;

		float _x2 = 0;
		float _y2 = 0;

		Color _color1, _color2;
		bool _cyclic;

		#region NativeObject

		GradientPaint _nativeObject = null;
		protected override Paint NativeObject {
			get {
				if ( _nativeObject == null )
					_nativeObject = new GradientPaint(
						_x1, _y1,
						new java.awt.Color(_color1.R, _color1.G, _color1.B, _color1.A),
						_x2, _y2, 
						new java.awt.Color(_color2.R, _color2.G, _color2.B, _color2.A), _cyclic);

				return _nativeObject;
			}
		}

		#endregion

		#region Initialization

		private void Init(float x1, float y1, Color color1, float x2, float y2, Color color2, bool cyclic) {
			_x1 = x1;
			_y1 = y1;
			_color1 = color1;

			_x2 = x2;
			_y2 = y2;
			_color2 = color2;

			_cyclic = cyclic;
			_nativeObject = null;
		}

		private void Init(float x1, float y1, Color color1, float x2, float y2, Color color2, float angle) {
			_gradientRectangle = new RectangleF(x1, y1, x2-x1, y2-y1);
			PointF [] points = GetMedianeEnclosingRect(x1, y1, x2, y2, angle);
			Init(points[0].X, points[0].Y, color1, points[1].X, points[1].Y, color2, false);
		}

		private void Init(float x1, float y1, Color color1, float x2, float y2, Color color2, LinearGradientMode linearGradientMode) {
			_gradientRectangle = new RectangleF(x1, y1, x2-x1, y2-y1);
			PointF [] points;

			switch (linearGradientMode) {
				case LinearGradientMode.Horizontal :
					Init(x1, y1, color1, x2, y1, color2, false);
					break;

				case LinearGradientMode.Vertical :
					Init(x1, y1, color1, x1, y2, color2, false);
					break;

				case LinearGradientMode.BackwardDiagonal :
					points = GetMedianeEnclosingRect(x1, y1, x2, y2, false);
					Init(points[0].X, points[0].Y, color2, points[1].X, points[1].Y, color1, false);
					break;

				case LinearGradientMode.ForwardDiagonal :
					points = GetMedianeEnclosingRect(x1, y1, x2, y2, true);
					Init(points[0].X, points[0].Y, color1, points[1].X, points[1].Y, color2, false);
					break;

				default :
					throw new ArgumentException("LinearGradientMode");
			}
		}

		#endregion

		#region Constructors

		private LinearGradientBrush (float x1, float y1, Color color1, float x2, float y2, Color color2, bool cyclic) {
			Init(x1, y1, color1, x2, y2, color2, cyclic);
		}

		internal LinearGradientBrush (float x1, float y1, Color color1, float x2, float y2, Color color2, LinearGradientMode mode) {
			Init(x1, y1, color1, x2, y2, color2, mode);
		}
		internal LinearGradientBrush (float x1, float y1, Color color1, float x2, float y2, Color color2) {
			Init(x1, y2, color1, x1, y2, color2, false);
		}
		public LinearGradientBrush (Point point1, Point point2, Color color1, Color color2) {
			_gradientRectangle = new RectangleF(point1.X, point1.Y, point2.X - point1.X, point2.Y - point2.Y);
			Init(point1.X, point1.Y, color1, point2.X, point2.Y, color2, false);
		}
		public LinearGradientBrush (PointF point1, PointF point2, Color color1, Color color2) {	
			_gradientRectangle = new RectangleF(point1.X, point1.Y, point2.X - point1.X, point2.Y - point2.Y);
			Init(point1.X, point1.Y, color1, point2.X, point2.Y, color2, false);
		}
		public LinearGradientBrush (Rectangle rect, Color color1, Color color2, LinearGradientMode linearGradientMode) {
			Init(rect.X, rect.Y, color1, rect.X + rect.Width, rect.Y + rect.Height, color2, linearGradientMode);
		}
		public LinearGradientBrush (RectangleF rect, Color color1, Color color2, LinearGradientMode linearGradientMode) {	
			Init(rect.X, rect.Y, color1, rect.X + rect.Width, rect.Y + rect.Height, color2, linearGradientMode);
		}
		public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle) {
			Init(rect.X, rect.Y, color1, rect.X + rect.Width, rect.Y + rect.Height, color2, angle);
		}
		public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle) {
			Init(rect.X, rect.Y, color1, rect.X + rect.Width, rect.Y + rect.Height, color2, angle);
		}
		public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle, bool isAngleScaleable):
			this(rect, color1, color2, angle) {
		}
		public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle, bool isAngleScaleable):
			this(rect, color1, color2, angle) {
		}
		#endregion

		#region GetMedianeEnclosingRect

		internal PointF [] GetMedianeEnclosingRect(float x1, float y1, float x2, float y2, float rotateAngle) {
			float width = x2 - x1;
			float height = y2 - y1;
			PointF rectCenter = new PointF(x1 + width/2, y1 + height/2);
			float gradLen = width * Math.Abs((float)Math.Cos(rotateAngle * Math.PI / 180)) + 
				height * Math.Abs((float)Math.Sin(rotateAngle * Math.PI / 180));

			PointF [] points = new PointF []{	new PointF(rectCenter.X - gradLen/2, rectCenter.Y),
												new PointF(rectCenter.X + gradLen/2, rectCenter.Y) };

			Matrix mx = new Matrix();
			mx.RotateAt((float)rotateAngle, rectCenter);
			mx.TransformPoints(points);
			return points;
		}
		internal PointF [] GetMedianeEnclosingRect(float x1, float y1, float x2, float y2, bool forwardDiagonal) {
			float width = x2 - x1;
			float height = y2 - y1;
			PointF rectCenter = new PointF(x1 + width/2, y1 + height/2);
			float rotateAngle = (float)Math.Atan2(width, height);
			float gradLen = width * (float)Math.Cos(rotateAngle);

			if (!forwardDiagonal)
				rotateAngle = -rotateAngle;
			
			PointF [] points = new PointF []{	new PointF(rectCenter.X - gradLen, rectCenter.Y),
												new PointF(rectCenter.X + gradLen, rectCenter.Y) };

			Matrix mx = new Matrix();
			mx.RotateAt((float)rotateAngle * (float)(180/Math.PI), rectCenter);
			mx.TransformPoints(points);
			return points;
		}

		#endregion

		#region Public Properties

		// FALLBACK: no functionality implemented for this property
		[MonoTODO]
		public Blend Blend {
			get {
				return _blend;
			}
			set {
				_blend = value;
			}
		}

		// FALLBACK: no functionality implemented for this property
		[MonoTODO]
		public bool GammaCorrection {
			get {
				return _gammaCorrection;
			}
			set {
				_gammaCorrection = value;
			}
		}

		// FALLBACK: functionality of two color gradient is implemented
		[MonoTODO]
		public ColorBlend InterpolationColors {
			get {
				return _interpolationColors;
			}
			set {
				if (value == null)
					throw new ArgumentNullException("ColorBlend");

				if ( (value.Colors == null) || (value.Colors.Length == 0) )
					throw new ArgumentException("ColorBlend");

				_interpolationColors = value;

				_color1 = value.Colors[0];
				_color2 = value.Colors[value.Colors.Length - 1];
				_nativeObject = null;
			}
		}

		public Color [] LinearColors {
			get {
				Color [] cl = new Color[2];
				cl[0] = _color1;
				cl[1] = _color2;
				return cl;
			}
			set {
				if (value == null)
					throw new ArgumentNullException("colors");

				_color1 = value[0];
				_color2 = value[1];
				_nativeObject = null;
			}
		}

		public RectangleF Rectangle {
			get {
				return _gradientRectangle;
			}
		}

		public Matrix Transform {
			get { return BrushTransform; }
			set { BrushTransform = value; }
		}

		// FALLBACK: not functionality implemented for this property
		[MonoTODO]
		public WrapMode WrapMode {
			get {
				return _wrapmode;
			}
			set {
				_wrapmode = value;
			}
		}
		#endregion

		#region Public Methods

		public void MultiplyTransform (Matrix matrix) {
			BrushMultiplyTransform(matrix, MatrixOrder.Prepend);
		}

		public void MultiplyTransform (Matrix matrix, MatrixOrder order) {
			BrushMultiplyTransform(matrix, order);			
		}

		public void ResetTransform () {
			BrushResetTransform();
		}

		public void RotateTransform (float angle) {
			BrushRotateTransform(angle, MatrixOrder.Prepend);
		}

		public void RotateTransform (float angle, MatrixOrder order) {
			BrushRotateTransform(angle, order);
		}

		public void ScaleTransform (float sx, float sy) {
			BrushScaleTransform(sx, sy, MatrixOrder.Prepend);
		}

		public void ScaleTransform (float sx, float sy, MatrixOrder order) {
			BrushScaleTransform(sx, sy, order);
		}

		public void SetBlendTriangularShape (float focus) {
			SetBlendTriangularShape (focus, 1.0F);
		}

		public void SetBlendTriangularShape (float focus, float scale) {
			_x2 = (_x1 + _x2) / 2;
			_y2 = (_y1 + _y2) / 2;
			_cyclic = true;
			_nativeObject = null;
		}

		public void SetSigmaBellShape (float focus) {
			SetSigmaBellShape (focus, 1.0F);
		}

		[MonoTODO]
		public void SetSigmaBellShape (float focus, float scale) {
			// FALLBACK: Triangle shape used
			SetBlendTriangularShape (focus, scale);
		}

		public void TranslateTransform (float dx, float dy) {
			BrushTranslateTransform (dx, dy);
		}

		public void TranslateTransform (float dx, float dy, MatrixOrder order) {
			BrushTranslateTransform(dx, dy, order);
		}

		public override object Clone () {
			LinearGradientBrush copy = (LinearGradientBrush)InternalClone();
			
			if (copy._nativeObject != null)
				copy._nativeObject = null;

			if (_interpolationColors != null) {
				copy._interpolationColors = new ColorBlend();
				if (_interpolationColors.Colors != null)
					copy._interpolationColors.Colors = (Color[])_interpolationColors.Colors.Clone();
				if (_interpolationColors.Positions != null)
					copy._interpolationColors.Positions = (float[])_interpolationColors.Positions.Clone();
			}

			if (_blend != null) {
				copy._blend = new Blend();
				if (_blend.Factors != null)
					copy._blend.Factors = (float[])_blend.Factors.Clone();
				if (_blend.Positions != null)
					copy._blend.Positions = (float[])_blend.Positions.Clone();
			}

			copy.LinearColors = (Color[])LinearColors.Clone();
			return copy;
		}
		#endregion
	}
}