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

namespace Svg
{
    public sealed class SvgColourServer : SvgPaintServer
    {
10
11
12
13
    	
    	/// <summary>
        /// An unspecified <see cref="SvgPaintServer"/>.
        /// </summary>
14
15
16
17
18
19
20
21
        public static readonly SvgPaintServer NotSet = new SvgColourServer(System.Drawing.Color.Black);
        /// <summary>
        /// A <see cref="SvgPaintServer"/> that should inherit from its parent.
        /// </summary>
        public static readonly SvgPaintServer Inherit = new SvgColourServer(System.Drawing.Color.Black);

        public SvgColourServer()
            : this(System.Drawing.Color.Black)
davescriven's avatar
davescriven committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
        {
        }

        public SvgColourServer(Color colour)
        {
            this._colour = colour;
        }

        private Color _colour;

        public Color Colour
        {
            get { return this._colour; }
            set { this._colour = value; }
        }

Eric Domke's avatar
Eric Domke committed
38
        public override Brush GetBrush(SvgVisualElement styleOwner, ISvgRenderer renderer, float opacity, bool forStroke = false)
davescriven's avatar
davescriven committed
39
        {
40
            //is none?
41
            if (this == SvgPaintServer.None) return new SolidBrush(System.Drawing.Color.Transparent);
Eric Domke's avatar
Eric Domke committed
42
                
43
            int alpha = (int)((opacity * (this.Colour.A/255.0f) ) * 255);
44
            Color colour = System.Drawing.Color.FromArgb(alpha, this.Colour);
davescriven's avatar
davescriven committed
45

46
            return new SolidBrush(colour);
davescriven's avatar
davescriven committed
47
48
49
50
        }

        public override string ToString()
        {
51
52
53
54
55
        	if(this == SvgPaintServer.None)
        		return "none";
        	else if(this == SvgColourServer.NotSet)
        		return "";
        	
davescriven's avatar
davescriven committed
56
57
58
59
            Color c = this.Colour;

            // Return the name if it exists
            if (c.IsKnownColor)
60
            {
davescriven's avatar
davescriven committed
61
                return c.Name;
62
            }
davescriven's avatar
davescriven committed
63
64
65
66

            // Return the hex value
            return String.Format("#{0}", c.ToArgb().ToString("x").Substring(2));
        }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82


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


		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgColourServer;
			newObj.Colour = this.Colour;
			return newObj;

		}

83
84
85
86
87
88
        public override bool Equals(object obj)
        {
            var objColor = obj as SvgColourServer;
            if (objColor == null)
                return false;

Eric Domke's avatar
Eric Domke committed
89
90
91
92
93
94
95
            if ((this == SvgPaintServer.None && obj != SvgPaintServer.None) ||
                (this != SvgPaintServer.None && obj == SvgPaintServer.None) ||
                (this == SvgColourServer.NotSet && obj != SvgColourServer.NotSet) ||
                (this != SvgColourServer.NotSet && obj == SvgColourServer.NotSet) ||
                (this == SvgColourServer.Inherit && obj != SvgColourServer.Inherit) ||
                (this != SvgColourServer.Inherit && obj == SvgColourServer.Inherit)) return false;

96
97
98
99
100
101
            return this.GetHashCode() == objColor.GetHashCode();
        }

        public override int GetHashCode()
        {
            return _colour.GetHashCode();
102
        }
davescriven's avatar
davescriven committed
103
104
    }
}