From e8fc731b8cde75a0af9571a3b491f6149038ea21 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sun, 27 Oct 2024 20:04:18 -0400 Subject: [PATCH] LibWeb/CSS: Introduce a base class for LCH-based color values This will be used by both CSSLCH and CSSOKLCH. --- .../Libraries/LibWeb/CSS/StyleValues/BUILD.gn | 2 +- Userland/Libraries/LibWeb/CMakeLists.txt | 2 +- .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 4 +-- .../{CSSOKLCH.cpp => CSSLCHLike.cpp} | 24 ++++++------- .../StyleValues/{CSSOKLCH.h => CSSLCHLike.h} | 34 ++++++++++++------- 5 files changed, 38 insertions(+), 28 deletions(-) rename Userland/Libraries/LibWeb/CSS/StyleValues/{CSSOKLCH.cpp => CSSLCHLike.cpp} (85%) rename Userland/Libraries/LibWeb/CSS/StyleValues/{CSSOKLCH.h => CSSLCHLike.h} (62%) diff --git a/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/StyleValues/BUILD.gn b/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/StyleValues/BUILD.gn index 1ea51c3dcd9..40ce7fdb276 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/StyleValues/BUILD.gn +++ b/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/StyleValues/BUILD.gn @@ -12,9 +12,9 @@ source_set("StyleValues") { "CSSHSL.cpp", "CSSHWB.cpp", "CSSKeywordValue.cpp", + "CSSLCHLike.cpp", "CSSLabLike.cpp", "CSSMathValue.cpp", - "CSSOKLCH.cpp", "CSSRGB.cpp", "ConicGradientStyleValue.cpp", "ContentStyleValue.cpp", diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 8b0f30de825..205decf3a0a 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -121,8 +121,8 @@ set(SOURCES CSS/StyleValues/CSSHWB.cpp CSS/StyleValues/CSSKeywordValue.cpp CSS/StyleValues/CSSLabLike.cpp + CSS/StyleValues/CSSLCHLike.cpp CSS/StyleValues/CSSMathValue.cpp - CSS/StyleValues/CSSOKLCH.cpp CSS/StyleValues/CSSRGB.cpp CSS/StyleValues/DisplayStyleValue.cpp CSS/StyleValues/EasingStyleValue.cpp diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index c749dc4b39f..846c0d7ce71 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -49,8 +49,8 @@ #include #include #include +#include #include -#include #include #include #include @@ -3399,7 +3399,7 @@ RefPtr Parser::parse_oklch_color_value(TokenStream(color_values[0].release_nonnull(), color_values[1].release_nonnull(), color_values[2].release_nonnull(), color_values[3].release_nonnull()); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/CSSOKLCH.cpp b/Userland/Libraries/LibWeb/CSS/StyleValues/CSSLCHLike.cpp similarity index 85% rename from Userland/Libraries/LibWeb/CSS/StyleValues/CSSOKLCH.cpp rename to Userland/Libraries/LibWeb/CSS/StyleValues/CSSLCHLike.cpp index f87f209f390..9d1f81c7987 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/CSSOKLCH.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/CSSLCHLike.cpp @@ -4,7 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include "CSSOKLCH.h" +#include "CSSLCHLike.h" #include #include #include @@ -14,6 +14,17 @@ namespace Web::CSS { +bool CSSLCHLike::equals(CSSStyleValue const& other) const +{ + if (type() != other.type()) + return false; + auto const& other_color = other.as_color(); + if (color_type() != other_color.color_type()) + return false; + auto const& other_oklch_like = verify_cast(other_color); + return m_properties == other_oklch_like.m_properties; +} + Color CSSOKLCH::to_color(Optional) const { auto const l_val = clamp(resolve_with_reference_value(m_properties.l, 1.0).value_or(0), 0, 1); @@ -24,17 +35,6 @@ Color CSSOKLCH::to_color(Optional) const return Color::from_oklab(l_val, c_val * cos(h_val), c_val * sin(h_val), alpha_val); } -bool CSSOKLCH::equals(CSSStyleValue const& other) const -{ - if (type() != other.type()) - return false; - auto const& other_color = other.as_color(); - if (color_type() != other_color.color_type()) - return false; - auto const& other_oklch = verify_cast(other_color); - return m_properties == other_oklch.m_properties; -} - // https://www.w3.org/TR/css-color-4/#serializing-oklab-oklch String CSSOKLCH::to_string() const { diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/CSSOKLCH.h b/Userland/Libraries/LibWeb/CSS/StyleValues/CSSLCHLike.h similarity index 62% rename from Userland/Libraries/LibWeb/CSS/StyleValues/CSSOKLCH.h rename to Userland/Libraries/LibWeb/CSS/StyleValues/CSSLCHLike.h index b66e145076d..7410ec60b04 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/CSSOKLCH.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/CSSLCHLike.h @@ -11,33 +11,29 @@ namespace Web::CSS { -// https://drafts.css-houdini.org/css-typed-om-1/#cssoklch -class CSSOKLCH final : public CSSColorValue { +class CSSLCHLike : public CSSColorValue { public: + template T> static ValueComparingNonnullRefPtr create(ValueComparingNonnullRefPtr l, ValueComparingNonnullRefPtr c, ValueComparingNonnullRefPtr h, ValueComparingRefPtr alpha = {}) { // alpha defaults to 1 if (!alpha) - return adopt_ref(*new (nothrow) CSSOKLCH(move(l), move(c), move(h), NumberStyleValue::create(1))); + alpha = NumberStyleValue::create(1); - return adopt_ref(*new (nothrow) CSSOKLCH(move(l), move(c), move(h), alpha.release_nonnull())); + return adopt_ref(*new (nothrow) T({}, move(l), move(c), move(h), alpha.release_nonnull())); } - virtual ~CSSOKLCH() override = default; + virtual ~CSSLCHLike() override = default; CSSStyleValue const& l() const { return *m_properties.l; } CSSStyleValue const& c() const { return *m_properties.c; } CSSStyleValue const& h() const { return *m_properties.h; } CSSStyleValue const& alpha() const { return *m_properties.alpha; } - virtual Color to_color(Optional) const override; - - String to_string() const override; - virtual bool equals(CSSStyleValue const& other) const override; -private: - CSSOKLCH(ValueComparingNonnullRefPtr l, ValueComparingNonnullRefPtr c, ValueComparingNonnullRefPtr h, ValueComparingNonnullRefPtr alpha) - : CSSColorValue(ColorType::OKLCH) +protected: + CSSLCHLike(ColorType color_type, ValueComparingNonnullRefPtr l, ValueComparingNonnullRefPtr c, ValueComparingNonnullRefPtr h, ValueComparingNonnullRefPtr alpha) + : CSSColorValue(color_type) , m_properties { .l = move(l), .c = move(c), .h = move(h), .alpha = move(alpha) } { } @@ -51,4 +47,18 @@ private: } m_properties; }; +// https://drafts.css-houdini.org/css-typed-om-1/#cssoklch +class CSSOKLCH final : public CSSLCHLike { +public: + CSSOKLCH(Badge, ValueComparingNonnullRefPtr l, ValueComparingNonnullRefPtr c, ValueComparingNonnullRefPtr h, ValueComparingNonnullRefPtr alpha) + : CSSLCHLike(ColorType::OKLCH, move(l), move(c), move(h), move(alpha)) + { + } + virtual ~CSSOKLCH() override = default; + + virtual Color to_color(Optional) const override; + + String to_string() const override; +}; + }