using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Svg { /// /// Converts string representations of colours into objects. /// public class SvgColourConverter : System.Drawing.ColorConverter { /// /// Converts the given object to the converter's native type. /// /// A that provides a format context. You can use this object to get additional information about the environment from which this converter is being invoked. /// A that specifies the culture to represent the color. /// The object to convert. /// /// An representing the converted value. /// /// The conversion cannot be performed. /// /// /// public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { string colour = value as string; if (colour != null) { colour = colour.Trim(); if (colour.StartsWith("rgb")) { try { int start = colour.IndexOf("(") + 1; string[] values = colour.Substring(start, colour.IndexOf(")") - start).Split(new char[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries); return System.Drawing.Color.FromArgb(int.Parse(values[0]), int.Parse(values[1]), int.Parse(values[2])); } catch { throw new SvgException("Colour is in an invalid format: '" + colour + "'"); } } else if (colour.StartsWith("#") && colour.Length == 4) { colour = string.Format(culture, "#{0}{0}{1}{1}{2}{2}", colour[1], colour[2], colour[3]); return base.ConvertFrom(context, culture, colour); } else { return base.ConvertFrom(context, culture, value); } } return base.ConvertFrom(context, culture, value); } } }