Svg.SvgColourConverter.ConvertFrom C# (CSharp) Method

ConvertFrom() public method

Converts the given object to the converter's native type.
The conversion cannot be performed.
public ConvertFrom ( System context, System culture, object value ) : object
context System 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.
culture System A that specifies the culture to represent the color.
value object The object to convert.
return object
        public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            string colour = value as string;

            if (colour != null)
            {
            	var oldCulture = Thread.CurrentThread.CurrentCulture;
            	Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
            	
                colour = colour.Trim();

                if (colour.StartsWith("rgb"))
                {
                    try
                    {
                        int start = colour.IndexOf("(") + 1;
                        
						//get the values from the RGB string
						string[] values = colour.Substring(start, colour.IndexOf(")") - start).Split(new char[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries);

						//determine the alpha value if this is an RGBA (it will be the 4th value if there is one)
						int alphaValue = 255;
						if (values.Length > 3)
						{
							//the alpha portion of the rgba is not an int 0-255 it is a decimal between 0 and 1
							//so we have to determine the corosponding byte value
							var alphastring = values[3];
							if(alphastring.StartsWith("."))
							{
								alphastring = "0" + alphastring;
							}
							
							var alphaDecimal = decimal.Parse(alphastring);
							
							if(alphaDecimal <= 1)
							{
								alphaValue = (int)(alphaDecimal * 255);
							}
							else
							{
								alphaValue = (int)alphaDecimal;
							}
						}
						Color colorpart = System.Drawing.Color.FromArgb(alphaValue, int.Parse(values[0]), int.Parse(values[1]), int.Parse(values[2]));

						return colorpart;
                    }
                    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);
                }
                
            	Thread.CurrentThread.CurrentCulture = oldCulture;
            }

            return base.ConvertFrom(context, culture, value);
        }

Usage Example

Esempio n. 1
0
        public static SvgPaintServer Create(string value, SvgDocument document)
        {
            // If it's pointing to a paint server
            if (string.IsNullOrEmpty(value))
            {
                return(SvgColourServer.NotSet);
            }
            else if (value == "inherit")
            {
                return(SvgColourServer.Inherit);
            }
            else if (value == "currentColor")
            {
                return(new SvgDeferredPaintServer(document, value));
            }
            else
            {
                var servers = new List <SvgPaintServer>();

                while (!string.IsNullOrEmpty(value))
                {
                    if (value.StartsWith("url(#"))
                    {
                        var leftParen = value.IndexOf(')', 5);
                        Uri id        = new Uri(value.Substring(5, leftParen - 5), UriKind.Relative);
                        value = value.Substring(leftParen + 1).Trim();
                        servers.Add((SvgPaintServer)document.IdManager.GetElementById(id));
                    }
                    // If referenced to to a different (linear or radial) gradient
                    else if (document.IdManager.GetElementById(value) != null && document.IdManager.GetElementById(value).GetType().BaseType == typeof(SvgGradientServer))
                    {
                        return((SvgPaintServer)document.IdManager.GetElementById(value));
                    }
                    else if (value.StartsWith("#")) // Otherwise try and parse as colour
                    {
                        switch (CountHexDigits(value, 1))
                        {
                        case 3:
                            servers.Add(new SvgColourServer((Color)_colourConverter.ConvertFrom(value.Substring(0, 4))));
                            value = value.Substring(4).Trim();
                            break;

                        case 6:
                            servers.Add(new SvgColourServer((Color)_colourConverter.ConvertFrom(value.Substring(0, 7))));
                            value = value.Substring(7).Trim();
                            break;

                        default:
                            return(new SvgDeferredPaintServer(document, value));
                        }
                    }
                    else
                    {
                        return(new SvgColourServer((Color)_colourConverter.ConvertFrom(value.Trim())));
                    }
                }

                if (servers.Count > 1)
                {
                    return(new SvgFallbackPaintServer(servers[0], servers.Skip(1)));
                }
                return(servers[0]);
            }
        }