-
-
Notifications
You must be signed in to change notification settings - Fork 95
Support CSS hex colors with alpha #510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
536a634
Fix CSS alpha hex paint parsing
wieslawsoltes 88e2602
Add alpha hex color regression tests
wieslawsoltes 0a01d5e
Fix animation color sentinel handling
wieslawsoltes c6748c7
Merge master into fix/issue-309-hex-alpha
wieslawsoltes af561f9
Address PR paint parsing feedback
wieslawsoltes 7370923
Ignore invalid CSS paint declarations
wieslawsoltes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Svg.Custom/Compatibility/SvgCssPaintDeclarationValidator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| #nullable enable | ||
|
|
||
| using System; | ||
|
|
||
| namespace Svg; | ||
|
|
||
| internal static class SvgCssPaintDeclarationValidator | ||
| { | ||
| public static bool ShouldIgnoreInvalidPaintDeclaration(SvgElement element, string name, string value) | ||
| { | ||
| if (!IsPaintProperty(name) || | ||
| ContainsVarFunction(value) || | ||
| IsCssWideKeyword(value)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| _ = SvgPaintServerFactory.Create(value, element.OwnerDocument); | ||
| return false; | ||
| } | ||
| catch (Exception ex) when (IsPaintConversionFailure(ex)) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| private static bool IsPaintProperty(string name) | ||
| { | ||
| return name.Equals("color", StringComparison.OrdinalIgnoreCase) || | ||
| name.Equals("fill", StringComparison.OrdinalIgnoreCase) || | ||
| name.Equals("flood-color", StringComparison.OrdinalIgnoreCase) || | ||
| name.Equals("lighting-color", StringComparison.OrdinalIgnoreCase) || | ||
| name.Equals("stop-color", StringComparison.OrdinalIgnoreCase) || | ||
| name.Equals("stroke", StringComparison.OrdinalIgnoreCase); | ||
| } | ||
|
|
||
| private static bool ContainsVarFunction(string value) | ||
| { | ||
| return value.IndexOf("var(", StringComparison.OrdinalIgnoreCase) >= 0; | ||
| } | ||
|
|
||
| private static bool IsCssWideKeyword(string value) | ||
| { | ||
| var trimmed = value.Trim(); | ||
| return trimmed.Equals("initial", StringComparison.OrdinalIgnoreCase) || | ||
| trimmed.Equals("inherit", StringComparison.OrdinalIgnoreCase) || | ||
| trimmed.Equals("unset", StringComparison.OrdinalIgnoreCase) || | ||
| trimmed.Equals("revert", StringComparison.OrdinalIgnoreCase) || | ||
| trimmed.Equals("revert-layer", StringComparison.OrdinalIgnoreCase); | ||
| } | ||
|
|
||
| private static bool IsPaintConversionFailure(Exception exception) | ||
| { | ||
| return exception is ArgumentException or FormatException or InvalidCastException or NotSupportedException; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a SMIL animation uses
#RGBA/#RRGGBBAAforfillorstrokevalues, this branch now lets the animation code treat those values as interpolatable colors. The interpolation path later serializes withnew SvgColourServer(color).ToString()inSvgAnimationController.TryInterpolateColor, butSvgColourServer.ToString()formatsToArgb().Substring(2), dropping alpha; for example, animating#0000to#0000writes#000000and renders opaque black instead of transparent. Please serialize interpolated colors through the paint converter or another alpha-preserving format whenA != 255.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in
af561f94d39672783c785f1ef195878414e702f5(Address PR paint parsing feedback).SvgAnimationControllernow serializes synthesized animation colors through a newFormatColorhelper. Opaque colors keep the existingSvgColourServer.ToString()output, while colors with alpha are emitted as#RRGGBBAA, which the paint converter can parse back without losing alpha. I replaced the interpolation, additive, and accumulation color paths that previously usednew SvgColourServer(color).ToString()and therefore dropped alpha.I added
SvgAnimationControllerTests.CreateAnimatedDocument_PreservesAlphaWhenInterpolatingHexAlphaPaint, which animatesfillfrom#00000000to#00000080and asserts the midpoint alpha is64, not opaque black.