SvgColourServer.cs 3.33 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);
42
43
44
45

            // default fill color is black, default stroke color is none
            if (this == SvgColourServer.NotSet) return new SolidBrush(forStroke ? System.Drawing.Color.Transparent : System.Drawing.Color.Black);

46
            int alpha = (int)Math.Round((opacity * (this.Colour.A/255.0) ) * 255);
47
            Color colour = System.Drawing.Color.FromArgb(alpha, this.Colour);
davescriven's avatar
davescriven committed
48

49
            return new SolidBrush(colour);
davescriven's avatar
davescriven committed
50
51
52
53
        }

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

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

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


		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;

		}

86
87
88
89
90
91
        public override bool Equals(object obj)
        {
            var objColor = obj as SvgColourServer;
            if (objColor == null)
                return false;

Eric Domke's avatar
Eric Domke committed
92
93
94
95
96
97
98
            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;

99
100
101
102
103
104
            return this.GetHashCode() == objColor.GetHashCode();
        }

        public override int GetHashCode()
        {
            return _colour.GetHashCode();
105
        }
davescriven's avatar
davescriven committed
106
107
    }
}