Skip to content

Commit

Permalink
Fix the color string format if no System.Drawing.Common is available (
Browse files Browse the repository at this point in the history
#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`
  • Loading branch information
mzh3511 authored May 21, 2023
1 parent 42faf6f commit b929430
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
25 changes: 24 additions & 1 deletion Source/Painting/SvgColourConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,30 @@ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext co
#endif
}

return base.ConvertTo(context, culture, value, destinationType);
return ToHtml((Color)value);
}

/// <summary>
/// Converts color to html string format.
/// Refer to https://source.dot.net/#System.Drawing.Primitives/System/Drawing/ColorTranslator.cs
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
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;
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions doc/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit b929430

Please sign in to comment.