diff --git a/Libraries/LibGfx/Color.cpp b/Libraries/LibGfx/Color.cpp index d980a4016cc..196ad25187d 100644 --- a/Libraries/LibGfx/Color.cpp +++ b/Libraries/LibGfx/Color.cpp @@ -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 diff --git a/Libraries/LibGfx/Color.h b/Libraries/LibGfx/Color.h index a6f2c3377de..e95cfbbcb3d 100644 --- a/Libraries/LibGfx/Color.h +++ b/Libraries/LibGfx/Color.h @@ -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); diff --git a/Libraries/LibWeb/CSS/StyleValues/CSSColor.cpp b/Libraries/LibWeb/CSS/StyleValues/CSSColor.cpp index 39703551ed8..5c3f58af9ea 100644 --- a/Libraries/LibWeb/CSS/StyleValues/CSSColor.cpp +++ b/Libraries/LibWeb/CSS/StyleValues/CSSColor.cpp @@ -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) 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(clamp(255 * c, 0, 255)); }; return Color(to_u8(c1), to_u8(c2), to_u8(c3), to_u8(alpha_val)); diff --git a/Libraries/LibWeb/CSS/StyleValues/CSSColor.h b/Libraries/LibWeb/CSS/StyleValues/CSSColor.h index 6e7f46a5523..433f7576db7 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 = { "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 c1, ValueComparingNonnullRefPtr c2, ValueComparingNonnullRefPtr c3, ValueComparingNonnullRefPtr alpha) diff --git a/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.h b/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.h index 5c771501357..3a2ae3e8793 100644 --- a/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.h +++ b/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.h @@ -24,6 +24,7 @@ public: enum class ColorType { RGB, // This is used by CSSRGB for rgb(...) and rgba(...). + A98RGB, HSL, HWB, Lab, diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-color/a98rgb-003-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-color/a98rgb-003-ref.html new file mode 100644 index 00000000000..9db28b6de5a --- /dev/null +++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-color/a98rgb-003-ref.html @@ -0,0 +1,11 @@ + + +CSS Color 4: CSS Color 4: a98-rgb + + +

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/a98rgb-004-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-color/a98rgb-004-ref.html new file mode 100644 index 00000000000..3b8771a704a --- /dev/null +++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-color/a98rgb-004-ref.html @@ -0,0 +1,10 @@ + + +CSS Color 4: CSS Color 4: a98-rgb + + +

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/a98rgb-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/a98rgb-001.html new file mode 100644 index 00000000000..c3eebbce359 --- /dev/null +++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/a98rgb-001.html @@ -0,0 +1,15 @@ + + +CSS Color 4: a98-rgb + + + + + + +

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

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

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

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

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/a98rgb-004.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/a98rgb-004.html new file mode 100644 index 00000000000..c43f0de5419 --- /dev/null +++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/a98rgb-004.html @@ -0,0 +1,17 @@ + + +CSS Color 4: a98-rgb + + + + + + +

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-007.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/predefined-007.html new file mode 100644 index 00000000000..74e43428d6a --- /dev/null +++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/predefined-007.html @@ -0,0 +1,17 @@ + + +CSS Color 4: predefined colorspaces, a98-rgb, 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-008.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/predefined-008.html new file mode 100644 index 00000000000..e3dbf307bda --- /dev/null +++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-color/predefined-008.html @@ -0,0 +1,17 @@ + + +CSS Color 4: predefined colorspaces, a98-rgb, percent values + + + + + + +

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

+

+

+