From a234a72b4d3dad09530d531494fe3385f0d9bd90 Mon Sep 17 00:00:00 2001 From: mrbean-bremen Date: Fri, 15 Feb 2019 21:08:15 +0100 Subject: [PATCH] Handle writing colors with alpha value - if the alpha value is less than 1, write additionally the opacity - fixes #129 (issue 4) --- Source/SvgElementFactory.cs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Source/SvgElementFactory.cs b/Source/SvgElementFactory.cs index ed86512..056f83f 100644 --- a/Source/SvgElementFactory.cs +++ b/Source/SvgElementFactory.cs @@ -264,14 +264,19 @@ namespace Svg try { - if (attributeName == "opacity" && attributeValue == "undefined") - { - attributeValue = "1"; - } - - descriptor.SetValue(element, descriptor.Converter.ConvertFrom(document, CultureInfo.InvariantCulture, attributeValue)); - - + if (attributeName == "opacity" && attributeValue == "undefined") + { + attributeValue = "1"; + } + var value = descriptor.Converter.ConvertFrom(document, CultureInfo.InvariantCulture, attributeValue); + if (value is SvgColourServer && ((SvgColourServer)value).Colour.A < 255) + { + // handle alpha values less than 1 by adding an opacity property + var opacity = ((SvgColourServer)value).Colour.A / 255.0; + SetPropertyValue(element, "opacity", + opacity.ToString("F2", CultureInfo.InvariantCulture), document, isStyle); + } + descriptor.SetValue(element, value); } catch { -- GitLab