SvgGradientStop.cs 3.27 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
16
17
18
    public class SvgGradientStop : SvgElement
    {
        private SvgUnit _offset;
        private Color _colour;
        private float _opacity;

19
20
21
        /// <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
22
23
24
25
        [SvgAttribute("offset")]
        public SvgUnit Offset
        {
            get { return this._offset; }
davescriven's avatar
davescriven committed
26
27
            set
            {
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
                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
54
            }
davescriven's avatar
davescriven committed
55
56
        }

57
58
59
        /// <summary>
        /// Gets or sets the colour of the gradient stop.
        /// </summary>
davescriven's avatar
davescriven committed
60
61
62
63
64
65
66
67
        [SvgAttribute("stop-color")]
        [TypeConverter(typeof(SvgColourConverter))]
        public Color Colour
        {
            get { return this._colour; }
            set { this._colour = value; }
        }

68
69
70
        /// <summary>
        /// Gets or sets the opacity of the gradient stop (0-1).
        /// </summary>
davescriven's avatar
davescriven committed
71
72
73
74
75
76
77
        [SvgAttribute("stop-opacity")]
        public float Opacity
        {
            get { return this._opacity; }
            set { this._opacity = value; }
        }

78
79
80
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgGradientStop"/> class.
        /// </summary>
davescriven's avatar
davescriven committed
81
82
83
84
85
86
87
        public SvgGradientStop()
        {
            this._offset = new SvgUnit(0.0f);
            this._colour = Color.Transparent;
            this._opacity = 1.0f;
        }

88
89
90
91
92
        /// <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
93
94
95
96
97
98
        public SvgGradientStop(SvgUnit offset, Color colour)
        {
            this._offset = offset;
            this._colour = colour;
            this._opacity = 1.0f;
        }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114


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

		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgGradientStop;
			newObj.Offset = this.Offset;
			newObj.Colour = this.Colour;
			newObj.Opacity = this.Opacity;

			return newObj;
		}
davescriven's avatar
davescriven committed
115
116
    }
}