LibWeb/CSS: Introduce a base class for LCH-based color values

This will be used by both CSSLCH and CSSOKLCH.
This commit is contained in:
Lucas CHOLLET 2024-10-27 20:04:18 -04:00 committed by Andreas Kling
parent 7a94709cd2
commit e8fc731b8c
Notes: github-actions[bot] 2024-10-28 22:38:10 +00:00
5 changed files with 38 additions and 28 deletions

View file

@ -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",

View file

@ -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

View file

@ -49,8 +49,8 @@
#include <LibWeb/CSS/StyleValues/CSSHSL.h>
#include <LibWeb/CSS/StyleValues/CSSHWB.h>
#include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
#include <LibWeb/CSS/StyleValues/CSSLCHLike.h>
#include <LibWeb/CSS/StyleValues/CSSLabLike.h>
#include <LibWeb/CSS/StyleValues/CSSOKLCH.h>
#include <LibWeb/CSS/StyleValues/CSSRGB.h>
#include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
#include <LibWeb/CSS/StyleValues/CounterDefinitionsStyleValue.h>
@ -3399,7 +3399,7 @@ RefPtr<CSSStyleValue> Parser::parse_oklch_color_value(TokenStream<ComponentValue
auto& color_values = *maybe_color_values;
return CSSOKLCH::create(color_values[0].release_nonnull(),
return CSSLCHLike::create<CSSOKLCH>(color_values[0].release_nonnull(),
color_values[1].release_nonnull(),
color_values[2].release_nonnull(),
color_values[3].release_nonnull());

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "CSSOKLCH.h"
#include "CSSLCHLike.h"
#include <AK/Math.h>
#include <AK/TypeCasts.h>
#include <LibWeb/CSS/Serialize.h>
@ -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<CSSLCHLike>(other_color);
return m_properties == other_oklch_like.m_properties;
}
Color CSSOKLCH::to_color(Optional<Layout::NodeWithStyle const&>) 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<Layout::NodeWithStyle const&>) 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<CSSOKLCH>(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
{

View file

@ -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<DerivedFrom<CSSLCHLike> T>
static ValueComparingNonnullRefPtr<CSSOKLCH> create(ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingNonnullRefPtr<CSSStyleValue> c, ValueComparingNonnullRefPtr<CSSStyleValue> h, ValueComparingRefPtr<CSSStyleValue> 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<Layout::NodeWithStyle const&>) const override;
String to_string() const override;
virtual bool equals(CSSStyleValue const& other) const override;
private:
CSSOKLCH(ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingNonnullRefPtr<CSSStyleValue> c, ValueComparingNonnullRefPtr<CSSStyleValue> h, ValueComparingNonnullRefPtr<CSSStyleValue> alpha)
: CSSColorValue(ColorType::OKLCH)
protected:
CSSLCHLike(ColorType color_type, ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingNonnullRefPtr<CSSStyleValue> c, ValueComparingNonnullRefPtr<CSSStyleValue> h, ValueComparingNonnullRefPtr<CSSStyleValue> 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<CSSLCHLike>, ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingNonnullRefPtr<CSSStyleValue> c, ValueComparingNonnullRefPtr<CSSStyleValue> h, ValueComparingNonnullRefPtr<CSSStyleValue> alpha)
: CSSLCHLike(ColorType::OKLCH, move(l), move(c), move(h), move(alpha))
{
}
virtual ~CSSOKLCH() override = default;
virtual Color to_color(Optional<Layout::NodeWithStyle const&>) const override;
String to_string() const override;
};
}