SvgColourServer.cs 2.13 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
14
15
    	
    	/// <summary>
        /// An unspecified <see cref="SvgPaintServer"/>.
        /// </summary>
        public static readonly SvgPaintServer NotSet = new SvgColourServer();
    	
16
        public SvgColourServer() : this(Color.Black)
davescriven's avatar
davescriven committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
        {
        }

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

        private Color _colour;

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

33
        public override Brush GetBrush(SvgVisualElement styleOwner, float opacity)
davescriven's avatar
davescriven committed
34
        {
35
            int alpha = (int)((opacity * (this.Colour.A/255.0f) ) * 255);
davescriven's avatar
davescriven committed
36
37
            Color colour = Color.FromArgb(alpha, this.Colour);

38
            return new SolidBrush(colour);
davescriven's avatar
davescriven committed
39
40
41
42
        }

        public override string ToString()
        {
43
44
45
46
47
        	if(this == SvgPaintServer.None)
        		return "none";
        	else if(this == SvgColourServer.NotSet)
        		return "";
        	
davescriven's avatar
davescriven committed
48
49
50
51
            Color c = this.Colour;

            // Return the name if it exists
            if (c.IsKnownColor)
52
            {
davescriven's avatar
davescriven committed
53
                return c.Name;
54
            }
davescriven's avatar
davescriven committed
55
56
57
58

            // Return the hex value
            return String.Format("#{0}", c.ToArgb().ToString("x").Substring(2));
        }
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74


		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;

		}

75
76
77
78
79
80
        public override bool Equals(object obj)
        {
            var objColor = obj as SvgColourServer;
            if (objColor == null)
                return false;

81
82
83
84
85
86
            return this.GetHashCode() == objColor.GetHashCode();
        }

        public override int GetHashCode()
        {
            return _colour.GetHashCode();
87
        }
davescriven's avatar
davescriven committed
88
89
    }
}