LibWeb/CSS: Tweak in CSSRGB::to_color() to avoid floating point errors

Example of the difference:

    50 * 2.55      --> 127.4999 --> round towards +∞ --> 127
    50 * 255 / 100 --> 127.5000 --> round towards +∞ --> 128

Now, 9 failing WPT tests in /css/css-color/ pass.
This commit is contained in:
ronak69 2024-10-18 09:40:13 +00:00 committed by Sam Atkins
commit 6c3ecf6a34
Notes: github-actions[bot] 2024-10-22 13:19:12 +00:00
3 changed files with 5 additions and 2 deletions

View file

@ -36,6 +36,9 @@
<div style="background-color: rgba(-20%, 0%, 300%, -50%);"> <div style="background-color: rgba(-20%, 0%, 300%, -50%);">
<p>legacy rgba with out-of-range percentages (should be clamped)</p> <p>legacy rgba with out-of-range percentages (should be clamped)</p>
</div> </div>
<div style="background-color: rgb(10% 50% 90%);">
<p>modern rgb with percentages (round away from 0)</p>
</div>
<div style="background-color: rgb(10 20% 30);"> <div style="background-color: rgb(10 20% 30);">
<p>modern rgb with mixed types</p> <p>modern rgb with mixed types</p>
</div> </div>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 93 KiB

After

Width:  |  Height:  |  Size: 80 KiB

Before After
Before After

View file

@ -25,14 +25,14 @@ Color CSSRGB::to_color(Optional<Layout::NodeWithStyle const&>) const
return normalized(style_value.as_number().number()); return normalized(style_value.as_number().number());
if (style_value.is_percentage()) if (style_value.is_percentage())
return normalized(style_value.as_percentage().value() * 2.55); return normalized(style_value.as_percentage().value() * 255 / 100);
if (style_value.is_math()) { if (style_value.is_math()) {
auto const& calculated = style_value.as_math(); auto const& calculated = style_value.as_math();
if (calculated.resolves_to_number()) if (calculated.resolves_to_number())
return normalized(calculated.resolve_number().value()); return normalized(calculated.resolve_number().value());
if (calculated.resolves_to_percentage()) if (calculated.resolves_to_percentage())
return normalized(calculated.resolve_percentage().value().value() * 2.55); return normalized(calculated.resolve_percentage().value().value() * 255 / 100);
} }
if (style_value.is_keyword() && style_value.to_keyword() == Keyword::None) if (style_value.is_keyword() && style_value.to_keyword() == Keyword::None)