From 63873f380999c749f7a81a67d8d0705c6c43c1c2 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Thu, 14 Nov 2024 22:43:43 -0500 Subject: [PATCH] LibWeb/CSS: Add support for the `rec2020` color space in `color()` This color space is often used as a reference in WPT tests, having support for it makes us pass 15 new tests: - css/css-color/rec2020-001.html - css/css-color/rec2020-002.html - css/css-color/rec2020-003.html - css/css-color/rec2020-004.html - css/css-color/rec2020-005.html - css/css-color/predefined-011.html - css/css-color/predefined-012.html --- Libraries/LibGfx/Color.cpp | 27 +++++++++++++++++++ Libraries/LibGfx/Color.h | 1 + Libraries/LibWeb/CSS/StyleValues/CSSColor.cpp | 5 ++++ Libraries/LibWeb/CSS/StyleValues/CSSColor.h | 2 +- .../LibWeb/CSS/StyleValues/CSSColorValue.h | 1 + .../css/css-color/rec2020-003-ref.html | 11 ++++++++ .../css/css-color/rec2020-004-ref.html | 10 +++++++ .../css/css-color/predefined-011.html | 17 ++++++++++++ .../css/css-color/predefined-012.html | 17 ++++++++++++ .../wpt-import/css/css-color/rec2020-001.html | 15 +++++++++++ .../wpt-import/css/css-color/rec2020-002.html | 15 +++++++++++ .../wpt-import/css/css-color/rec2020-003.html | 18 +++++++++++++ .../wpt-import/css/css-color/rec2020-004.html | 17 ++++++++++++ .../wpt-import/css/css-color/rec2020-005.html | 15 +++++++++++ 14 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 Tests/LibWeb/Ref/expected/wpt-import/css/css-color/rec2020-003-ref.html create mode 100644 Tests/LibWeb/Ref/expected/wpt-import/css/css-color/rec2020-004-ref.html create mode 100644 Tests/LibWeb/Ref/input/wpt-import/css/css-color/predefined-011.html create mode 100644 Tests/LibWeb/Ref/input/wpt-import/css/css-color/predefined-012.html create mode 100644 Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-001.html create mode 100644 Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-002.html create mode 100644 Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-003.html create mode 100644 Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-004.html create mode 100644 Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-005.html diff --git a/Libraries/LibGfx/Color.cpp b/Libraries/LibGfx/Color.cpp index 399481b5e45..69ce28204b4 100644 --- a/Libraries/LibGfx/Color.cpp +++ b/Libraries/LibGfx/Color.cpp @@ -476,6 +476,33 @@ Color Color::from_pro_photo_rgb(float r, float g, float b, float alpha) return from_xyz50(x, y, z, alpha); } +// https://www.w3.org/TR/css-color-4/#predefined-rec2020 +Color Color::from_rec2020(float r, float g, float b, float alpha) +{ + auto to_linear = [](float c) -> float { + auto constexpr alpha = 1.09929682680944; + auto constexpr beta = 0.018053968510807; + + u8 sign = c < 0 ? -1 : 1; + auto absolute = abs(c); + + if (absolute < beta * 4.5) + return c / 4.5; + + return sign * (pow((absolute + alpha - 1) / alpha, 1 / 0.45)); + }; + + auto linear_r = to_linear(r); + auto linear_g = to_linear(g); + auto linear_b = to_linear(b); + + float x = 0.63695805 * linear_r + 0.14461690 * linear_g + 0.16888098 * linear_b; + float y = 0.26270021 * linear_r + 0.67799807 * linear_g + 0.05930172 * linear_b; + float z = 0.00000000 * linear_r + 0.02807269 * linear_g + 1.06098506 * 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 diff --git a/Libraries/LibGfx/Color.h b/Libraries/LibGfx/Color.h index f278a5bf5fc..d7115c847aa 100644 --- a/Libraries/LibGfx/Color.h +++ b/Libraries/LibGfx/Color.h @@ -187,6 +187,7 @@ public: static Color from_lab(float L, float a, float b, float alpha = 1.0f); static Color from_linear_srgb(float r, float g, float b, float alpha = 1.0f); static Color from_pro_photo_rgb(float r, float g, float b, float alpha = 1.0f); + static Color from_rec2020(float r, float g, 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); diff --git a/Libraries/LibWeb/CSS/StyleValues/CSSColor.cpp b/Libraries/LibWeb/CSS/StyleValues/CSSColor.cpp index 91811f21ecb..dda417f263a 100644 --- a/Libraries/LibWeb/CSS/StyleValues/CSSColor.cpp +++ b/Libraries/LibWeb/CSS/StyleValues/CSSColor.cpp @@ -25,6 +25,8 @@ CSSColorValue::ColorType color_type_from_string_view(StringView color_space) return CSSColorValue::ColorType::sRGBLinear; if (color_space == "prophoto-rgb"sv) return CSSColorValue::ColorType::ProPhotoRGB; + if (color_space == "rec2020"sv) + return CSSColorValue::ColorType::Rec2020; if (color_space == "xyz-d50"sv) return CSSColorValue::ColorType::XYZD50; if (color_space == "xyz"sv || color_space == "xyz-d65") @@ -88,6 +90,9 @@ Color CSSColor::to_color(Optional) const if (color_type() == ColorType::ProPhotoRGB) return Color::from_pro_photo_rgb(c1, c2, c3, alpha_val); + if (color_type() == ColorType::Rec2020) + return Color::from_rec2020(c1, c2, c3, alpha_val); + if (color_type() == ColorType::XYZD50) return Color::from_xyz50(c1, c2, c3, alpha_val); diff --git a/Libraries/LibWeb/CSS/StyleValues/CSSColor.h b/Libraries/LibWeb/CSS/StyleValues/CSSColor.h index c54cf824ceb..f1c4addd347 100644 --- a/Libraries/LibWeb/CSS/StyleValues/CSSColor.h +++ b/Libraries/LibWeb/CSS/StyleValues/CSSColor.h @@ -21,7 +21,7 @@ public: virtual Color to_color(Optional) const override; virtual String to_string() const override; - static constexpr Array s_supported_color_space = { "a98-rgb"sv, "display-p3"sv, "srgb"sv, "srgb-linear"sv, "prophoto-rgb"sv, "xyz"sv, "xyz-d50"sv, "xyz-d65"sv }; + static constexpr Array s_supported_color_space = { "a98-rgb"sv, "display-p3"sv, "srgb"sv, "srgb-linear"sv, "prophoto-rgb"sv, "rec2020"sv, "xyz"sv, "xyz-d50"sv, "xyz-d65"sv }; private: CSSColor(ColorType color_type, ValueComparingNonnullRefPtr c1, ValueComparingNonnullRefPtr c2, ValueComparingNonnullRefPtr c3, ValueComparingNonnullRefPtr alpha) diff --git a/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.h b/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.h index 415c2ad20c4..0e57d0e0038 100644 --- a/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.h +++ b/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.h @@ -35,6 +35,7 @@ public: sRGB, // This is used by CSSColor for color(srgb ...). sRGBLinear, ProPhotoRGB, + Rec2020, XYZD50, XYZD65, }; diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-color/rec2020-003-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-color/rec2020-003-ref.html new file mode 100644 index 00000000000..da95008fcf4 --- /dev/null +++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-color/rec2020-003-ref.html @@ -0,0 +1,11 @@ + + +CSS Color 4: CSS Color 4: rec2020 + + +

Test passes if you see a single square, and not two rectangles of different colors.

+
+ diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-color/rec2020-004-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-color/rec2020-004-ref.html new file mode 100644 index 00000000000..68068041f9f --- /dev/null +++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-color/rec2020-004-ref.html @@ -0,0 +1,10 @@ + + +CSS Color 4: CSS Color 4: rec2020 + + +

Test passes if you see a single square, and not two rectangles of different colors.

+
+ diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-color/predefined-011.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/predefined-011.html new file mode 100644 index 00000000000..ea33d294845 --- /dev/null +++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/predefined-011.html @@ -0,0 +1,17 @@ + + +CSS Color 4: predefined colorspaces, rec2020, decimal values + + + + + + +

Test passes if you see a green square, and no red.

+

+

+ diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-color/predefined-012.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/predefined-012.html new file mode 100644 index 00000000000..a74f82e115f --- /dev/null +++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/predefined-012.html @@ -0,0 +1,17 @@ + + +CSS Color 4: predefined colorspaces, rec2020, percent values + + + + + + +

Test passes if you see a green square, and no red.

+

+

+ diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-001.html new file mode 100644 index 00000000000..40665071a7b --- /dev/null +++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-001.html @@ -0,0 +1,15 @@ + + +CSS Color 4: rec2020 + + + + + + +

Test passes if you see a green square, and no red.

+
+ diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-002.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-002.html new file mode 100644 index 00000000000..5b34d83c6c3 --- /dev/null +++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-002.html @@ -0,0 +1,15 @@ + + +CSS Color 4: rec2020 + + + + + + +

Test passes if you see a black square, and no red.

+
+ diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-003.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-003.html new file mode 100644 index 00000000000..3bf95bb4813 --- /dev/null +++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-003.html @@ -0,0 +1,18 @@ + + +CSS Color 4: rec2020 + + + + + + +

Test passes if you see a single square, and not two rectangles of different colors.

+
+
+ diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-004.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-004.html new file mode 100644 index 00000000000..6624b4aecc9 --- /dev/null +++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-004.html @@ -0,0 +1,17 @@ + + +CSS Color 4: rec2020 + + + + + + +

Test passes if you see a single square, and not two rectangles of different colors.

+
+
+ diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-005.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-005.html new file mode 100644 index 00000000000..d749b3f84fe --- /dev/null +++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/rec2020-005.html @@ -0,0 +1,15 @@ + + +CSS Color 4: rec2020 + + + + + + +

Test passes if you see a green square, and no red.

+
+