5 using System.Runtime.InteropServices;
8 namespace System.Drawing.Drawing2D {
10 /// Summary description for PathGradientBrush.
13 public sealed class PathGradientBrush : Brush {
19 RectangleF _rectangle;
20 Color [] _surroundColors;
21 ColorBlend _interpolationColors;
23 GraphicsPath _texturePath;
24 bool _triangularShape = false;
26 protected override java.awt.Paint NativeObject {
34 void Initialize(GraphicsPath path, WrapMode wrapMode, bool initColors, bool calcCenter) {
38 _rectangle = path.GetBounds();
41 _centerColor = Color.Black;
42 _surroundColors = new Color []{ Color.White };
45 Bitmap texture = new Bitmap( (int)_rectangle.Width, (int)_rectangle.Height );
46 Graphics g = Graphics.FromImage( texture );
47 PointF [] pathPoints = path.PathPoints;
50 for (int i=0; i < pathPoints.Length; i++) {
51 _center.X += pathPoints[i].X;
52 _center.Y += pathPoints[i].Y;
54 _center.X /= pathPoints.Length;
55 _center.Y /= pathPoints.Length;
59 DrawSector( g, CenterPoint, pathPoints[pathPoints.Length-1], pathPoints[0], CenterColor, SurroundColors[outerColor] );
60 for(int i=0; i < pathPoints.Length - 1; i++) {
61 if (outerColor < SurroundColors.Length - 1)
63 DrawSector( g, CenterPoint, pathPoints[i], pathPoints[i+1], CenterColor, SurroundColors[outerColor] );
66 _nativeObject = new TextureBrush( texture );
68 private void DrawSector(Graphics g, PointF center, PointF p1, PointF p2, Color innerColor, Color outerColor) {
69 GraphicsPath pt = new GraphicsPath();
70 pt.AddLine(p1, center);
71 pt.AddLine(center, p2);
72 LinearGradientBrush lgb = new LinearGradientBrush( GetVertical(center, p1, p2) , center, outerColor, innerColor );
74 lgb.SetBlendTriangularShape(0.5f);
75 g.FillPath( lgb, pt );
77 private PointF GetVertical(PointF c, PointF p1, PointF p2) {
\r
79 return new PointF(p1.X, c.Y);
\r
81 return new PointF(c.X, p2.Y);
\r
83 float a = (float)(p2.Y - p1.Y) / (p2.X - p1.X);
\r
86 float b1 = p1.Y - a * p1.X;
\r
87 float b2 = c.Y - av * c.X;
\r
89 float ox = (b1 - b2) / (av - a);
\r
90 float oy = av * ox + b2;
\r
92 return new PointF(ox, oy);
\r
99 public PathGradientBrush (GraphicsPath path) {
100 Initialize( path, WrapMode.Clamp, true, true );
103 public PathGradientBrush (Point [] points) : this (points, WrapMode.Clamp) {
106 public PathGradientBrush (PointF [] points) : this (points, WrapMode.Clamp) {
109 public PathGradientBrush (Point [] points, WrapMode wrapMode) {
110 GraphicsPath path = new GraphicsPath();
111 path.AddLines( points );
112 Initialize( path, wrapMode, true, true );
115 public PathGradientBrush (PointF [] points, WrapMode wrapMode) {
116 GraphicsPath path = new GraphicsPath();
117 path.AddLines( points );
118 Initialize( path, wrapMode, true, true );
135 public Color CenterColor {
140 _centerColor = value;
141 Initialize(_texturePath, _wrapMode, false, false );
145 public PointF CenterPoint {
151 Initialize(_texturePath, _wrapMode, false, false );
155 public PointF FocusScales {
164 public ColorBlend InterpolationColors {
166 return _interpolationColors;
169 _interpolationColors = value;
173 public RectangleF Rectangle {
179 public Color [] SurroundColors {
181 return _surroundColors;
184 _surroundColors = value;
185 Initialize(_texturePath, _wrapMode, false, false );
189 public Matrix Transform {
191 return BrushTransform;
194 BrushTransform = value;
198 public WrapMode WrapMode {
211 public void MultiplyTransform (Matrix matrix) {
212 base.BrushMultiplyTransform( matrix );
215 public void MultiplyTransform (Matrix matrix, MatrixOrder order) {
216 base.BrushMultiplyTransform( matrix, order );
219 public void ResetTransform () {
220 base.BrushResetTransform();
223 public void RotateTransform (float angle) {
224 base.BrushRotateTransform( angle );
227 public void RotateTransform (float angle, MatrixOrder order) {
228 base.BrushRotateTransform( angle, order );
231 public void ScaleTransform (float sx, float sy) {
232 base.BrushScaleTransform( sx, sy );
235 public void ScaleTransform (float sx, float sy, MatrixOrder order) {
236 base.BrushScaleTransform( sx, sy, order );
239 public void SetBlendTriangularShape (float focus) {
240 SetBlendTriangularShape (focus, 1.0F);
243 public void SetBlendTriangularShape (float focus, float scale) {
244 _triangularShape = true;
245 Initialize( _texturePath, _wrapMode, false, false );
248 public void SetSigmaBellShape (float focus) {
249 SetSigmaBellShape (focus, 1.0F);
253 public void SetSigmaBellShape (float focus, float scale) {
254 // FALLBACK: Triangle shape used
255 SetBlendTriangularShape (focus, scale);
258 public void TranslateTransform (float dx, float dy) {
259 base.BrushTranslateTransform( dx, dy );
262 public void TranslateTransform (float dx, float dy, MatrixOrder order) {
263 base.BrushTranslateTransform( dx, dy, order );
266 public override object Clone () {
267 PathGradientBrush copy = (PathGradientBrush)InternalClone();
269 if (copy._nativeObject != null)
270 copy._nativeObject = (Brush)copy._nativeObject.Clone();
272 if (copy._surroundColors != null)
273 copy._surroundColors = (Color[])copy._surroundColors.Clone();
275 if (copy._texturePath != null)
276 copy._texturePath = (GraphicsPath)copy._texturePath.Clone();
278 //TBD: clone _blend, _interpolationColors
279 //copy._blend = copy._blend