From b92943078f97954c0b1141fef18f2d07bb52207c Mon Sep 17 00:00:00 2001 From: mzh3511 <287972920@qq.com> Date: Sun, 21 May 2023 19:13:55 +0800 Subject: [PATCH] Fix the color string format if no `System.Drawing.Common` is available (#1055) * in case no `System.Drawing.Common` is present, the `SvgColourConverter.ConvertTo` method returned invalid SVG that cannot be correctly displayed in current browsers * fix by handling the case independent of `System.Drawing.Common` --- Source/Painting/SvgColourConverter.cs | 25 ++++++++++++++++++++++++- doc/ReleaseNotes.md | 5 +++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Source/Painting/SvgColourConverter.cs b/Source/Painting/SvgColourConverter.cs index 8146daf56..ab84fbbf1 100644 --- a/Source/Painting/SvgColourConverter.cs +++ b/Source/Painting/SvgColourConverter.cs @@ -204,7 +204,30 @@ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext co #endif } - return base.ConvertTo(context, culture, value, destinationType); + return ToHtml((Color)value); + } + + /// + /// Converts color to html string format. + /// Refer to https://source.dot.net/#System.Drawing.Primitives/System/Drawing/ColorTranslator.cs + /// + /// + /// + private static string ToHtml(Color c) + { + var colorString = string.Empty; + if (c.IsEmpty) + return colorString; + + if (c.IsNamedColor) + { + colorString = c == Color.LightGray ? "LightGrey" : c.Name; + colorString = colorString.ToLowerInvariant(); + } + else + colorString = $"#{c.R:X2}{c.G:X2}{c.B:X2}"; + + return colorString; } /// diff --git a/doc/ReleaseNotes.md b/doc/ReleaseNotes.md index c9dc1f571..7455beeb4 100644 --- a/doc/ReleaseNotes.md +++ b/doc/ReleaseNotes.md @@ -8,8 +8,9 @@ The release versions are NuGet releases. ### Fixes * fixed build error in C# 11 (see [PR #1030](https://github.com/svg-net/SVG/pull/1030)) -* fixed out of memory exception on SVGs with gradients (see [PR #1038] (https://github.com/svg-net/SVG/pull/1038)) -* fixed missing styles when `DeepCopy` the `SvgElement` (see [PR #1053] (https://github.com/svg-net/SVG/pull/1053)) +* fixed out of memory exception on SVGs with gradients (see [PR #1038](https://github.com/svg-net/SVG/pull/1038)) +* fixed missing styles when `DeepCopy` the `SvgElement` (see [PR #1053](https://github.com/svg-net/SVG/pull/1053)) +* fix the color string format incompatible with the Edge/Chrome browsers in case of no System.Drawing.Common (see [PR #1055](https://github.com/svg-net/SVG/pull/1055)) ## [Version 3.4.4](https://www.nuget.org/packages/Svg/3.4.4) (2022-10-29)