1 | using System;
|
---|
2 | using System.Windows.Media;
|
---|
3 |
|
---|
4 | using SharpVectors.Dom.Svg;
|
---|
5 | using SharpVectors.Dom.Css;
|
---|
6 |
|
---|
7 | namespace SharpVectors.Renderers.Wpf
|
---|
8 | {
|
---|
9 | public sealed class WpfSvgColor : SvgColor
|
---|
10 | {
|
---|
11 | private string _propertyName;
|
---|
12 | private SvgStyleableElement _element;
|
---|
13 |
|
---|
14 | public WpfSvgColor(SvgStyleableElement elm, string propertyName)
|
---|
15 | : base(elm.GetComputedStyle("").GetPropertyValue(propertyName))
|
---|
16 | {
|
---|
17 | _element = elm;
|
---|
18 | _propertyName = propertyName;
|
---|
19 | }
|
---|
20 |
|
---|
21 | public Color Color
|
---|
22 | {
|
---|
23 | get
|
---|
24 | {
|
---|
25 | SvgColor colorToUse;
|
---|
26 | if (ColorType == SvgColorType.CurrentColor)
|
---|
27 | {
|
---|
28 | string sCurColor = _element.GetComputedStyle("").GetPropertyValue("color");
|
---|
29 | colorToUse = new SvgColor(sCurColor);
|
---|
30 | }
|
---|
31 | else if (ColorType == SvgColorType.Unknown)
|
---|
32 | {
|
---|
33 | colorToUse = new SvgColor("black");
|
---|
34 | }
|
---|
35 | else
|
---|
36 | {
|
---|
37 | colorToUse = this;
|
---|
38 | }
|
---|
39 |
|
---|
40 | ICssColor rgbColor = colorToUse.RgbColor;
|
---|
41 | int red = Convert.ToInt32(rgbColor.Red.GetFloatValue(CssPrimitiveType.Number));
|
---|
42 | int green = Convert.ToInt32(rgbColor.Green.GetFloatValue(CssPrimitiveType.Number));
|
---|
43 | int blue = Convert.ToInt32(rgbColor.Blue.GetFloatValue(CssPrimitiveType.Number));
|
---|
44 |
|
---|
45 | return Color.FromArgb((byte)this.Alpha, (byte)red, (byte)green, (byte)blue);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public int Alpha
|
---|
50 | {
|
---|
51 | get
|
---|
52 | {
|
---|
53 | string propName;
|
---|
54 | if (_propertyName.Equals("stop-color"))
|
---|
55 | {
|
---|
56 | propName = "stop-opacity";
|
---|
57 | }
|
---|
58 | else if (_propertyName.Equals("flood-color"))
|
---|
59 | {
|
---|
60 | propName = "flood-opacity";
|
---|
61 | }
|
---|
62 | else
|
---|
63 | {
|
---|
64 | return 255;
|
---|
65 | }
|
---|
66 |
|
---|
67 | double alpha = 255;
|
---|
68 | string opacity;
|
---|
69 |
|
---|
70 | opacity = _element.GetPropertyValue(propName);
|
---|
71 | if (opacity.Length > 0)
|
---|
72 | alpha *= SvgNumber.ParseNumber(opacity);
|
---|
73 |
|
---|
74 | alpha = Math.Min(alpha, 255);
|
---|
75 | alpha = Math.Max(alpha, 0);
|
---|
76 |
|
---|
77 | return Convert.ToInt32(alpha);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public double Opacity
|
---|
82 | {
|
---|
83 | get
|
---|
84 | {
|
---|
85 | string propName;
|
---|
86 | if (_propertyName.Equals("stop-color"))
|
---|
87 | {
|
---|
88 | propName = "stop-opacity";
|
---|
89 | }
|
---|
90 | else if (_propertyName.Equals("flood-color"))
|
---|
91 | {
|
---|
92 | propName = "flood-opacity";
|
---|
93 | }
|
---|
94 | else
|
---|
95 | {
|
---|
96 | return 1.0f;
|
---|
97 | }
|
---|
98 |
|
---|
99 | double alpha = 1.0f;
|
---|
100 | string opacity;
|
---|
101 |
|
---|
102 | opacity = _element.GetPropertyValue(propName);
|
---|
103 | if (opacity != null && opacity.Length > 0)
|
---|
104 | {
|
---|
105 | alpha = SvgNumber.ParseNumber(opacity);
|
---|
106 | }
|
---|
107 |
|
---|
108 | alpha = Math.Min(alpha, 1.0f);
|
---|
109 | alpha = Math.Max(alpha, 0.0f);
|
---|
110 |
|
---|
111 | return alpha;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|