LibWeb/CSS: Add support for the a98-rgb color space in color()

This makes us pass the following WPT tests:
 - css/css-color/a98rgb-001.html
 - css/css-color/a98rgb-002.html
 - css/css-color/a98rgb-003.html
 - css/css-color/a98rgb-004.html
 - css/css-color/predefined-007.html
 - css/css-color/predefined-008.html
This commit is contained in:
Lucas CHOLLET 2024-11-13 23:17:40 -05:00 committed by Andreas Kling
parent c0ae3aa884
commit a59d9a3986
Notes: github-actions[bot] 2024-11-16 09:31:06 +00:00
13 changed files with 146 additions and 1 deletions

View file

@ -413,6 +413,24 @@ Color Color::from_linear_srgb(float red, float green, float blue, float alpha)
clamp(lroundf(alpha * 255.f), 0, 255));
}
// https://www.w3.org/TR/css-color-4/#predefined-a98-rgb
Color Color::from_a98rgb(float r, float g, float b, float alpha)
{
auto to_linear = [](float c) {
return pow(c, 563. / 256);
};
auto linear_r = to_linear(r);
auto linear_g = to_linear(g);
auto linear_b = to_linear(b);
float x = 0.57666904 * linear_r + 0.18555824 * linear_g + 0.18822865 * linear_b;
float y = 0.29734498 * linear_r + 0.62736357 * linear_g + 0.07529146 * linear_b;
float z = 0.02703136 * linear_r + 0.07068885 * linear_g + 0.99133754 * linear_b;
return from_xyz65(x, y, z, alpha);
}
Color Color::from_xyz50(float x, float y, float z, float alpha)
{
// See commit description for these values

View file

@ -182,6 +182,7 @@ public:
return Color(r_u8, g_u8, b_u8, a_u8);
}
static Color from_a98rgb(float r, float g, float b, float alpha = 1.0f);
static Color from_lab(float L, float a, float b, float alpha = 1.0f);
static Color from_xyz50(float x, float y, float z, float alpha = 1.0f);
static Color from_xyz65(float x, float y, float z, float alpha = 1.0f);

View file

@ -15,6 +15,8 @@ namespace {
CSSColorValue::ColorType color_type_from_string_view(StringView color_space)
{
if (color_space == "a98-rgb"sv)
return CSSColorValue::ColorType::A98RGB;
if (color_space == "srgb"sv)
return CSSColorValue::ColorType::sRGB;
if (color_space == "srgb-linear"sv)
@ -65,6 +67,9 @@ Color CSSColor::to_color(Optional<Layout::NodeWithStyle const&>) const
auto const c3 = resolve_with_reference_value(m_properties.channels[2], 1).value_or(0);
auto const alpha_val = resolve_alpha(m_properties.alpha).value_or(1);
if (color_type() == ColorType::A98RGB)
return Color::from_a98rgb(c1, c2, c3, alpha_val);
if (color_type() == ColorType::sRGB) {
auto const to_u8 = [](float c) -> u8 { return round_to<u8>(clamp(255 * c, 0, 255)); };
return Color(to_u8(c1), to_u8(c2), to_u8(c3), to_u8(alpha_val));

View file

@ -21,7 +21,7 @@ public:
virtual Color to_color(Optional<Layout::NodeWithStyle const&>) const override;
virtual String to_string() const override;
static constexpr Array s_supported_color_space = { "srgb"sv, "srgb-linear"sv, "xyz"sv, "xyz-d50"sv, "xyz-d65"sv };
static constexpr Array s_supported_color_space = { "a98-rgb"sv, "srgb"sv, "srgb-linear"sv, "xyz"sv, "xyz-d50"sv, "xyz-d65"sv };
private:
CSSColor(ColorType color_type, ValueComparingNonnullRefPtr<CSSStyleValue> c1, ValueComparingNonnullRefPtr<CSSStyleValue> c2, ValueComparingNonnullRefPtr<CSSStyleValue> c3, ValueComparingNonnullRefPtr<CSSStyleValue> alpha)

View file

@ -24,6 +24,7 @@ public:
enum class ColorType {
RGB, // This is used by CSSRGB for rgb(...) and rgba(...).
A98RGB,
HSL,
HWB,
Lab,

View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>CSS Color 4: CSS Color 4: a98-rgb</title>
<style>
body { background-color: grey; }
.test { background-color: rgb(99.993% 100% 100%); width: 12em; height: 12em; } /* color(a98-rgb 1 1 1) converted to sRGB */
</style>
<body>
<p>Test passes if you see a single square, and not two rectangles of different colors.</p>
<div class="test"></div>
</body>

View file

@ -0,0 +1,10 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>CSS Color 4: CSS Color 4: a98-rgb</title>
<style>
.test { background-color: lab(83.2141% -129.1072 87.1718); width: 12em; height: 12em; } /* color(a98-rgb 0 1 0) converted to Lab */
</style>
<body>
<p>Test passes if you see a single square, and not two rectangles of different colors.</p>
<div class="test"></div>
</body>

View file

@ -0,0 +1,15 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>CSS Color 4: a98-rgb</title>
<link rel="author" title="Sam Weinig" href="mailto:weinig@apple.com">
<link rel="help" href="https://drafts.csswg.org/css-color-4/#valdef-color-a98-rgb">
<link rel="match" href="../../../../expected/wpt-import/css/css-color/greensquare-ref.html">
<meta name="assert" content="a98-rgb with no alpha">
<style>
.test { background-color: red; width: 12em; height: 12em; }
.test { background-color: color(a98-rgb 0.281363 0.498012 0.116746); } /* green (sRGB #008000) converted to a98-rgb */
</style>
<body>
<p>Test passes if you see a green square, and no red.</p>
<div class="test"></div>
</body>

View file

@ -0,0 +1,15 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>CSS Color 4: a98-rgb</title>
<link rel="author" title="Sam Weinig" href="mailto:weinig@apple.com">
<link rel="help" href="https://drafts.csswg.org/css-color-4/#valdef-color-a98-rgb">
<link rel="match" href="../../../../expected/wpt-import/css/css-color/blacksquare-ref.html">
<meta name="assert" content="a98-rgb with no alpha">
<style>
.test { background-color: red; width: 12em; height: 12em; }
.test { background-color: color(a98-rgb 0 0 0); } /* black (sRGB #000000) converted to a98-rgb */
</style>
<body>
<p>Test passes if you see a black square, and no red.</p>
<div class="test"></div>
</body>

View file

@ -0,0 +1,18 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>CSS Color 4: a98-rgb</title>
<link rel="author" title="Sam Weinig" href="mailto:weinig@apple.com">
<link rel="help" href="https://drafts.csswg.org/css-color-4/#valdef-color-a98-rgb">
<link rel="match" href="../../../../expected/wpt-import/css/css-color/a98rgb-003-ref.html">
<meta name="assert" content="a98-rgb with no alpha">
<style>
body { background-color: grey; }
.test { background-color: red; width: 12em; height: 6em; margin-top: 0; }
.ref { background-color: rgb(99.993% 100% 100%); width: 12em; height: 6em; margin-bottom: 0; } /* color(a98-rgb 1 1 1) converted to sRGB */
.test { background-color: color(a98-rgb 1 1 1); }
</style>
<body>
<p>Test passes if you see a single square, and not two rectangles of different colors.</p>
<div class="ref"></div>
<div class="test"></div>
</body>

View file

@ -0,0 +1,17 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>CSS Color 4: a98-rgb</title>
<link rel="author" title="Sam Weinig" href="mailto:weinig@apple.com">
<link rel="help" href="https://drafts.csswg.org/css-color-4/#valdef-color-a98-rgb">
<link rel="match" href="../../../../expected/wpt-import/css/css-color/a98rgb-004-ref.html">
<meta name="assert" content="a98-rgb with no alpha">
<style>
.test { background-color: red; width: 12em; height: 6em; margin-top: 0; }
.ref { background-color: lab(83.2141% -129.1072 87.1718); width: 12em; height: 6em; margin-bottom: 0; } /* color(a98-rgb 0 1 0) converted to Lab */
.test { background-color: color(a98-rgb 0 1 0); }
</style>
<body>
<p>Test passes if you see a single square, and not two rectangles of different colors.</p>
<div class="ref"></div>
<div class="test"></div>
</body>

View file

@ -0,0 +1,17 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>CSS Color 4: predefined colorspaces, a98-rgb, decimal values</title>
<link rel="author" title="Chris Lilley" href="mailto:chris@w3.org">
<link rel="help" href="https://drafts.csswg.org/css-color-4/#predefined">
<link rel="match" href="../../../../expected/wpt-import/css/css-color/greensquare-090-ref.html">
<meta name="assert" content="Color function with explicit a98-rgb value as decimal matches sRGB #009900">
<style>
.test { background-color: red; width: 12em; height: 6em; margin-top:0}
.ref { background-color: #009900; width: 12em; height: 6em; margin-bottom: 0}
.test {background-color: color(a98-rgb 0.33582 0.59441 0.13934)}
</style>
<body>
<p>Test passes if you see a green square, and no red.</p>
<p class="ref"> </p>
<p class="test"> </p>
</body>

View file

@ -0,0 +1,17 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>CSS Color 4: predefined colorspaces, a98-rgb, percent values</title>
<link rel="author" title="Chris Lilley" href="mailto:chris@w3.org">
<link rel="help" href="https://drafts.csswg.org/css-color-4/#predefined">
<link rel="match" href="../../../../expected/wpt-import/css/css-color/greensquare-090-ref.html">
<meta name="assert" content="Color function with explicit a98-rgb value as percent matches sRGB #009900">
<style>
.test { background-color: red; width: 12em; height: 6em; margin-top:0}
.ref { background-color: #009900; width: 12em; height: 6em; margin-bottom: 0}
.test {background-color: color(a98-rgb 33.582% 59.441% 13.934%)}
</style>
<body>
<p>Test passes if you see a green square, and no red.</p>
<p class="ref"> </p>
<p class="test"> </p>
</body>