SvgRadialGradientServer.cs 2.85 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Svg
{
9
    [SvgElement("radialGradient")]
davescriven's avatar
davescriven committed
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
    public sealed class SvgRadialGradientServer : SvgGradientServer
    {
        [SvgAttribute("cx")]
        public SvgUnit CenterX
        {
            get { return this.Attributes.GetAttribute<SvgUnit>("cx"); }
            set { this.Attributes["cx"] = value; }
        }

        [SvgAttribute("cy")]
        public SvgUnit CenterY
        {
            get { return this.Attributes.GetAttribute<SvgUnit>("cy"); }
            set { this.Attributes["cy"] = value; }
        }

        [SvgAttribute("r")]
        public SvgUnit Radius
        {
            get { return this.Attributes.GetAttribute<SvgUnit>("r"); }
            set { this.Attributes["r"] = value; }
        }

        [SvgAttribute("fx")]
        public SvgUnit FocalX
        {
            get { return this.Attributes.GetAttribute<SvgUnit>("fx"); }
            set { this.Attributes["fx"] = value; }
        }

        [SvgAttribute("fy")]
        public SvgUnit FocalY
        {
            get { return this.Attributes.GetAttribute<SvgUnit>("fy"); }
            set { this.Attributes["fy"] = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgRadialGradientServer"/> class.
        /// </summary>
        public SvgRadialGradientServer()
        {
52
53
54
55
            //Apply default values of 50% to cX,cY and r
            CenterX = new SvgUnit(SvgUnitType.Percentage, 50);
            CenterY = new SvgUnit(SvgUnitType.Percentage, 50);
            Radius = new SvgUnit(SvgUnitType.Percentage, 50);
davescriven's avatar
davescriven committed
56
57
        }

58
        public override Brush GetBrush(SvgVisualElement renderingElement, float opacity)
davescriven's avatar
davescriven committed
59
60
61
62
63
        {
            GraphicsPath path = new GraphicsPath();
            float left = this.CenterX.ToDeviceValue(renderingElement);
            float top = this.CenterY.ToDeviceValue(renderingElement, true);
            float radius = this.Radius.ToDeviceValue(renderingElement);
64
            RectangleF boundingBox = (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox) ? renderingElement.Bounds : renderingElement.OwnerDocument.GetDimensions();
davescriven's avatar
davescriven committed
65

66
67
68
            if (radius > 0)
            {
                path.AddEllipse(left - radius, top - radius, radius * 2, radius * 2);
davescriven's avatar
davescriven committed
69

70
71
                PathGradientBrush brush = new PathGradientBrush(path);
                ColorBlend blend = base.GetColourBlend(renderingElement, opacity);
davescriven's avatar
davescriven committed
72

73
74
75
76
77
78
79
                brush.InterpolationColors = blend;
                brush.CenterPoint = new PointF(this.FocalX.ToDeviceValue(renderingElement), this.FocalY.ToDeviceValue(renderingElement, true));

                return brush;
            }
            
            return null;            
davescriven's avatar
davescriven committed
80
81
82
        }
    }
}