SvgGradientStop.cs 3.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.ComponentModel;

namespace Svg
{
9
10
11
    /// <summary>
    /// Represents a colour stop in a gradient.
    /// </summary>
12
    [SvgElement("stop")]
davescriven's avatar
davescriven committed
13
14
15
    public class SvgGradientStop : SvgElement
    {
        private SvgUnit _offset;
Eric Domke's avatar
Eric Domke committed
16
        
17
18
19
        /// <summary>
        /// Gets or sets the offset, i.e. where the stop begins from the beginning, of the gradient stop.
        /// </summary>
davescriven's avatar
davescriven committed
20
21
22
23
        [SvgAttribute("offset")]
        public SvgUnit Offset
        {
            get { return this._offset; }
davescriven's avatar
davescriven committed
24
25
            set
            {
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
                SvgUnit unit = value;

                if (value.Type == SvgUnitType.Percentage)
                {
                    if (value.Value > 100)
                    {
                        unit = new SvgUnit(value.Type, 100);
                    }
                    else if (value.Value < 0)
                    {
                        unit = new SvgUnit(value.Type, 0);
                    }
                }
                else if (value.Type == SvgUnitType.User)
                {
                    if (value.Value > 1)
                    {
                        unit = new SvgUnit(value.Type, 1);
                    }
                    else if (value.Value < 0)
                    {
                        unit = new SvgUnit(value.Type, 0);
                    }
                }

                this._offset = unit.ToPercentage();
davescriven's avatar
davescriven committed
52
            }
davescriven's avatar
davescriven committed
53
54
        }

55
56
57
        /// <summary>
        /// Gets or sets the colour of the gradient stop.
        /// </summary>
davescriven's avatar
davescriven committed
58
        [SvgAttribute("stop-color")]
59
        [TypeConverter(typeof(SvgPaintServerFactory))]
Eric Domke's avatar
Eric Domke committed
60
        public SvgPaintServer StopColor
davescriven's avatar
davescriven committed
61
        {
Eric Domke's avatar
Eric Domke committed
62
63
64
65
66
67
68
            get 
            {
                var direct = this.Attributes.GetAttribute<SvgPaintServer>("stop-color", SvgColourServer.NotSet);
                if (direct == SvgColourServer.Inherit) return this.Attributes["stop-color"] as SvgPaintServer ?? SvgColourServer.NotSet;
                return direct;
            }
            set { this.Attributes["stop-color"] = value; }
davescriven's avatar
davescriven committed
69
70
        }

71
72
73
        /// <summary>
        /// Gets or sets the opacity of the gradient stop (0-1).
        /// </summary>
davescriven's avatar
davescriven committed
74
        [SvgAttribute("stop-opacity")]
Eric Domke's avatar
Eric Domke committed
75
        public string Opacity
davescriven's avatar
davescriven committed
76
        {
Eric Domke's avatar
Eric Domke committed
77
78
79
80
81
82
83
84
            get { return this.Attributes["stop-opacity"] as string; }
            set { this.Attributes["stop-opacity"] = value; }
        }

        public float GetOpacity()
        {
            var opacity = this.Opacity;
            return string.IsNullOrEmpty(opacity) ? 1.0f : float.Parse(opacity);
davescriven's avatar
davescriven committed
85
86
        }

87
88
89
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgGradientStop"/> class.
        /// </summary>
davescriven's avatar
davescriven committed
90
91
92
93
94
        public SvgGradientStop()
        {
            this._offset = new SvgUnit(0.0f);
        }

95
96
97
98
99
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgGradientStop"/> class.
        /// </summary>
        /// <param name="offset">The offset.</param>
        /// <param name="colour">The colour.</param>
davescriven's avatar
davescriven committed
100
101
102
103
        public SvgGradientStop(SvgUnit offset, Color colour)
        {
            this._offset = offset;
        }
104

105
106
        public Color GetColor(SvgElement parent)
        {
Eric Domke's avatar
Eric Domke committed
107
            var core = SvgDeferredPaintServer.TryGet<SvgColourServer>(this.StopColor, parent);
108
109
110
            if (core == null) throw new InvalidOperationException("Invalid paint server for gradient stop detected.");
            return core.Colour;
        }
111
112
113
114
115
116
117
118
119
120
121
122

		public override SvgElement DeepCopy()
		{
			return DeepCopy<SvgGradientStop>();
		}

		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgGradientStop;
			newObj.Offset = this.Offset;
			return newObj;
		}
davescriven's avatar
davescriven committed
123
124
    }
}