Skip to content

Commit

Permalink
rgb() colors that use none should use hex syntax for serialization in…
Browse files Browse the repository at this point in the history
… canvas

https://bugs.webkit.org/show_bug.cgi?id=278392

Reviewed by Darin Adler.

When using HTML serialization (such as in canvas code), `rgb(none none none)`
should serialize as `#000000`, not `rgb(0, 0, 0)` as it currently does.

This is more consistent and matches other browsers.

* LayoutTests/fast/canvas/canvas-color-serialization.html:
* LayoutTests/fast/canvas/canvas-color-serialization-expected.txt:
    - Add serialization tests. Additional will be added in WPT
      via web-platform-tests/wpt#47148.

* Source/WebCore/platform/graphics/ColorSerialization.cpp:
(WebCore::serializationForHTML):
    - Make sure to call through to the HTML serializer for legacy
      rgb() colors that have none (which is indicated by the use
      of SRGBA<float> without the "useColorFunctionSerialization"
      bit set).

Canonical link: https://commits.webkit.org/283120@main
  • Loading branch information
weinig committed Sep 3, 2024
1 parent b982f2a commit b7e7afa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
12 changes: 12 additions & 0 deletions LayoutTests/fast/canvas/canvas-color-serialization-expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ PASS trySettingShadowColor('logical') is '#666666'
PASS trySettingStrokeStyle('visual') is '#666666'
PASS trySettingFillStyle('visual') is '#666666'
PASS trySettingShadowColor('visual') is '#666666'
PASS trySettingStrokeStyle('rgb(none none none)') is '#000000'
PASS trySettingFillStyle('rgb(none none none)') is '#000000'
PASS trySettingShadowColor('rgb(none none none)') is '#000000'
PASS trySettingStrokeStyle('rgb(300 none 400)') is '#ff00ff'
PASS trySettingFillStyle('rgb(300 none 400)') is '#ff00ff'
PASS trySettingShadowColor('rgb(300 none 400)') is '#ff00ff'
PASS trySettingStrokeColorWithSetter('transparent') is 'rgba(0, 0, 0, 0)'
PASS trySettingFillColorWithSetter('transparent') is 'rgba(0, 0, 0, 0)'
PASS trySettingShadowWithSetter('transparent') is 'rgba(0, 0, 0, 0)'
Expand Down Expand Up @@ -162,6 +168,12 @@ PASS trySettingShadowWithSetter('RgB(1,2,3)') is '#010203'
PASS trySettingStrokeColorWithSetter('rGbA(1,2,3,0)') is 'rgba(1, 2, 3, 0)'
PASS trySettingFillColorWithSetter('rGbA(1,2,3,0)') is 'rgba(1, 2, 3, 0)'
PASS trySettingShadowWithSetter('rGbA(1,2,3,0)') is 'rgba(1, 2, 3, 0)'
PASS trySettingStrokeColorWithSetter('rgb(none none none)') is '#000000'
PASS trySettingFillColorWithSetter('rgb(none none none)') is '#000000'
PASS trySettingShadowWithSetter('rgb(none none none)') is '#000000'
PASS trySettingStrokeColorWithSetter('rgb(300 none 400)') is '#ff00ff'
PASS trySettingFillColorWithSetter('rgb(300 none 400)') is '#ff00ff'
PASS trySettingShadowWithSetter('rgb(300 none 400)') is '#ff00ff'
PASS tryClearShadowAfterSettingColor('red') is 'rgba(0, 0, 0, 0)'
PASS tryClearShadowAfterSettingColor('rgba(0, 5, 10, 0.4)') is 'rgba(0, 0, 0, 0)'
PASS successfullyParsed is true
Expand Down
4 changes: 4 additions & 0 deletions LayoutTests/fast/canvas/canvas-color-serialization.html
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@
trySettingColor("'content'", "'#666666'");
trySettingColor("'logical'", "'#666666'");
trySettingColor("'visual'", "'#666666'");
trySettingColor("'rgb(none none none)'", "'#000000'");
trySettingColor("'rgb(300 none 400)'", "'#ff00ff'");

trySettingColorWithSetter("'transparent'", "'rgba(0, 0, 0, 0)'");
trySettingColorWithSetter("'red'", "'#ff0000'");
Expand All @@ -224,6 +226,8 @@
trySettingColorWithSetter("'rgba(1,2,3,0.4)'", "'rgba(1, 2, 3, 0.4)'");
trySettingColorWithSetter("'RgB(1,2,3)'", "'#010203'");
trySettingColorWithSetter("'rGbA(1,2,3,0)'", "'rgba(1, 2, 3, 0)'");
trySettingColorWithSetter("'rgb(none none none)'", "'#000000'");
trySettingColorWithSetter("'rgb(300 none 400)'", "'#ff00ff'");

shouldBe("tryClearShadowAfterSettingColor('red')", "'rgba(0, 0, 0, 0)'");
shouldBe("tryClearShadowAfterSettingColor('rgba(0, 5, 10, 0.4)')", "'rgba(0, 0, 0, 0)'");
Expand Down
5 changes: 4 additions & 1 deletion Source/WebCore/platform/graphics/ColorSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,10 @@ String serializationForCSS(const SRGBA<float>& color, bool useColorFunctionSeria

String serializationForHTML(const SRGBA<float>& color, bool useColorFunctionSerialization)
{
return serializationForCSS(color, useColorFunctionSerialization);
if (useColorFunctionSerialization)
return serializationUsingColorFunction(color);

return serializationForHTML(convertColor<SRGBA<uint8_t>>(color), false);
}

String serializationForRenderTreeAsText(const SRGBA<float>& color, bool useColorFunctionSerialization)
Expand Down

0 comments on commit b7e7afa

Please sign in to comment.