5 using System.Runtime.InteropServices;
8 namespace System.Drawing.Drawing2D {
10 /// Summary description for PathGradientBrush.
12 public sealed class PathGradientBrush : Brush {
18 RectangleF _rectangle;
19 Color [] _surroundColors;
20 ColorBlend _interpolationColors;
22 GraphicsPath _texturePath;
23 bool _triangularShape = false;
25 protected override java.awt.Paint NativeObject {
33 void Initialize(GraphicsPath path, WrapMode wrapMode, bool initColors, bool calcCenter) {
37 _rectangle = path.GetBounds();
40 _centerColor = Color.Black;
41 _surroundColors = new Color []{ Color.White };
44 Bitmap texture = new Bitmap( (int)_rectangle.Width, (int)_rectangle.Height );
45 Graphics g = Graphics.FromImage( texture );
46 PointF [] pathPoints = path.PathPoints;
49 for (int i=0; i < pathPoints.Length; i++) {
50 _center.X += pathPoints[i].X;
51 _center.Y += pathPoints[i].Y;
53 _center.X /= pathPoints.Length;
54 _center.Y /= pathPoints.Length;
58 DrawSector( g, CenterPoint, pathPoints[pathPoints.Length-1], pathPoints[0], CenterColor, SurroundColors[outerColor] );
59 for(int i=0; i < pathPoints.Length - 1; i++) {
60 if (outerColor < SurroundColors.Length - 1)
62 DrawSector( g, CenterPoint, pathPoints[i], pathPoints[i+1], CenterColor, SurroundColors[outerColor] );
65 _nativeObject = new TextureBrush( texture );
67 private void DrawSector(Graphics g, PointF center, PointF p1, PointF p2, Color innerColor, Color outerColor) {
68 GraphicsPath pt = new GraphicsPath();
69 pt.AddLine(p1, center);
70 pt.AddLine(center, p2);
71 LinearGradientBrush lgb = new LinearGradientBrush( GetVertical(center, p1, p2) , center, outerColor, innerColor );
73 lgb.SetBlendTriangularShape(0.5f);
74 g.FillPath( lgb, pt );
76 private PointF GetVertical(PointF c, PointF p1, PointF p2) {
\r
78 return new PointF(p1.X, c.Y);
\r
80 return new PointF(c.X, p2.Y);
\r
82 float a = (float)(p2.Y - p1.Y) / (p2.X - p1.X);
\r
85 float b1 = p1.Y - a * p1.X;
\r
86 float b2 = c.Y - av * c.X;
\r
88 float ox = (b1 - b2) / (av - a);
\r
89 float oy = av * ox + b2;
\r
91 return new PointF(ox, oy);
\r
98 public PathGradientBrush (GraphicsPath path) {
99 Initialize( path, WrapMode.Clamp, true, true );
102 public PathGradientBrush (Point [] points) : this (points, WrapMode.Clamp) {
105 public PathGradientBrush (PointF [] points) : this (points, WrapMode.Clamp) {
108 public PathGradientBrush (Point [] points, WrapMode wrapMode) {
109 GraphicsPath path = new GraphicsPath();
110 path.AddLines( points );
111 Initialize( path, wrapMode, true, true );
114 public PathGradientBrush (PointF [] points, WrapMode wrapMode) {
115 GraphicsPath path = new GraphicsPath();
116 path.AddLines( points );
117 Initialize( path, wrapMode, true, true );
133 public Color CenterColor {
138 _centerColor = value;
139 Initialize(_texturePath, _wrapMode, false, false );
143 public PointF CenterPoint {
149 Initialize(_texturePath, _wrapMode, false, false );
153 public PointF FocusScales {
162 public ColorBlend InterpolationColors {
164 return _interpolationColors;
167 _interpolationColors = value;
171 public RectangleF Rectangle {
177 public Color [] SurroundColors {
179 return _surroundColors;
182 _surroundColors = value;
183 Initialize(_texturePath, _wrapMode, false, false );
187 public Matrix Transform {
189 return BrushTransform;
192 BrushTransform = value;
196 public WrapMode WrapMode {
209 public void MultiplyTransform (Matrix matrix) {
210 base.BrushMultiplyTransform( matrix );
213 public void MultiplyTransform (Matrix matrix, MatrixOrder order) {
214 base.BrushMultiplyTransform( matrix, order );
217 public void ResetTransform () {
218 base.BrushResetTransform();
221 public void RotateTransform (float angle) {
222 base.BrushRotateTransform( angle );
225 public void RotateTransform (float angle, MatrixOrder order) {
226 base.BrushRotateTransform( angle, order );
229 public void ScaleTransform (float sx, float sy) {
230 base.BrushScaleTransform( sx, sy );
233 public void ScaleTransform (float sx, float sy, MatrixOrder order) {
234 base.BrushScaleTransform( sx, sy, order );
237 public void SetBlendTriangularShape (float focus) {
238 SetBlendTriangularShape (focus, 1.0F);
241 public void SetBlendTriangularShape (float focus, float scale) {
242 _triangularShape = true;
243 Initialize( _texturePath, _wrapMode, false, false );
246 public void SetSigmaBellShape (float focus) {
247 SetSigmaBellShape (focus, 1.0F);
250 public void SetSigmaBellShape (float focus, float scale) {
251 // FALLBACK: Triangle shape used
252 SetBlendTriangularShape (focus, scale);
255 public void TranslateTransform (float dx, float dy) {
256 base.BrushTranslateTransform( dx, dy );
259 public void TranslateTransform (float dx, float dy, MatrixOrder order) {
260 base.BrushTranslateTransform( dx, dy, order );
263 public override object Clone () {
264 PathGradientBrush copy = (PathGradientBrush)InternalClone();
266 if (copy._nativeObject != null)
267 copy._nativeObject = (Brush)copy._nativeObject.Clone();
269 if (copy._surroundColors != null)
270 copy._surroundColors = (Color[])copy._surroundColors.Clone();
272 if (copy._texturePath != null)
273 copy._texturePath = (GraphicsPath)copy._texturePath.Clone();
275 //TBD: clone _blend, _interpolationColors
276 //copy._blend = copy._blend