Click here to Skip to main content
15,894,539 members
Articles / Desktop Programming / WPF

SharpVectors - SVG# Reloaded: An Introduction

Rate me:
Please Sign up or sign in to vote.
4.98/5 (33 votes)
17 Nov 2010BSD10 min read 205.8K   21.7K   101  
A C# library for converting SVG to WPF and viewing SVG files in WPF Applications
using System;
using System.Windows.Media;

using SharpVectors.Dom.Svg;
using SharpVectors.Dom.Css;

namespace SharpVectors.Renderers.Wpf
{
	public sealed class WpfSvgColor : SvgColor
	{
        private string _propertyName;
		private SvgStyleableElement _element;

		public WpfSvgColor(SvgStyleableElement elm, string propertyName) 
            : base(elm.GetComputedStyle("").GetPropertyValue(propertyName))
		{
			_element      = elm;
			_propertyName = propertyName;
		}

        public Color Color
        {
            get
            {
                SvgColor colorToUse;
                if (ColorType == SvgColorType.CurrentColor)
                {
                    string sCurColor = _element.GetComputedStyle("").GetPropertyValue("color");
                    colorToUse = new SvgColor(sCurColor);
                }
                else if (ColorType == SvgColorType.Unknown)
                {
                    colorToUse = new SvgColor("black");
                }
                else
                {
                    colorToUse = this;
                }

                ICssColor rgbColor = colorToUse.RgbColor;
                int red   = Convert.ToInt32(rgbColor.Red.GetFloatValue(CssPrimitiveType.Number));
                int green = Convert.ToInt32(rgbColor.Green.GetFloatValue(CssPrimitiveType.Number));
                int blue  = Convert.ToInt32(rgbColor.Blue.GetFloatValue(CssPrimitiveType.Number));

                return Color.FromArgb((byte)this.Alpha, (byte)red, (byte)green, (byte)blue);
            }
        }

        public int Alpha
        {   
            get
            {
                string propName;
                if (_propertyName.Equals("stop-color"))
                {
                    propName = "stop-opacity";
                }
                else if (_propertyName.Equals("flood-color"))
                {
                    propName = "flood-opacity";
                }
                else
                {
                    return 255;
                }

                double alpha = 255;
                string opacity;

                opacity = _element.GetPropertyValue(propName);
                if (opacity.Length > 0)
                    alpha *= SvgNumber.ParseNumber(opacity);

                alpha = Math.Min(alpha, 255);
                alpha = Math.Max(alpha, 0);

                return Convert.ToInt32(alpha);
            }
        }

        public double Opacity
		{
            get
            {
                string propName;
                if (_propertyName.Equals("stop-color"))
                {
                    propName = "stop-opacity";
                }
                else if (_propertyName.Equals("flood-color"))
                {
                    propName = "flood-opacity";
                }
                else
                {
                    return 1.0f;
                }

                double alpha = 1.0f;
                string opacity;

                opacity = _element.GetPropertyValue(propName);
                if (opacity != null && opacity.Length > 0)
                {
                    alpha = SvgNumber.ParseNumber(opacity);
                }

                alpha = Math.Min(alpha, 1.0f);
                alpha = Math.Max(alpha, 0.0f);

                return alpha;
            }
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Engineer
Japan Japan
Systems Engineer

Comments and Discussions