LibWeb/CSS: Make CSSOKLab inherit from a new CSSLabLike class

This class provides the common base for both CSSOKLab and the future
CSSLab classes.
This commit is contained in:
Lucas CHOLLET 2024-10-26 16:14:04 -04:00 committed by Andreas Kling
parent 707bcaf604
commit 8efa046e0c
Notes: github-actions[bot] 2024-10-27 09:28:40 +00:00
5 changed files with 42 additions and 33 deletions

View file

@ -11,9 +11,9 @@ source_set("StyleValues") {
"CSSHSL.cpp",
"CSSHWB.cpp",
"CSSKeywordValue.cpp",
"CSSLabLike.cpp",
"CSSMathValue.cpp",
"CSSOKLCH.cpp",
"CSSOKLab.cpp",
"CSSRGB.cpp",
"ConicGradientStyleValue.cpp",
"ContentStyleValue.cpp",

View file

@ -119,8 +119,8 @@ set(SOURCES
CSS/StyleValues/CSSHSL.cpp
CSS/StyleValues/CSSHWB.cpp
CSS/StyleValues/CSSKeywordValue.cpp
CSS/StyleValues/CSSLabLike.cpp
CSS/StyleValues/CSSMathValue.cpp
CSS/StyleValues/CSSOKLab.cpp
CSS/StyleValues/CSSOKLCH.cpp
CSS/StyleValues/CSSRGB.cpp
CSS/StyleValues/DisplayStyleValue.cpp

View file

@ -48,8 +48,8 @@
#include <LibWeb/CSS/StyleValues/CSSHSL.h>
#include <LibWeb/CSS/StyleValues/CSSHWB.h>
#include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
#include <LibWeb/CSS/StyleValues/CSSLabLike.h>
#include <LibWeb/CSS/StyleValues/CSSOKLCH.h>
#include <LibWeb/CSS/StyleValues/CSSOKLab.h>
#include <LibWeb/CSS/StyleValues/CSSRGB.h>
#include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
#include <LibWeb/CSS/StyleValues/CounterDefinitionsStyleValue.h>

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "CSSOKLab.h"
#include "CSSLabLike.h"
#include <AK/TypeCasts.h>
#include <LibWeb/CSS/Serialize.h>
#include <LibWeb/CSS/StyleValues/CSSMathValue.h>
@ -13,6 +13,17 @@
namespace Web::CSS {
bool CSSLabLike::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_lab_like = verify_cast<CSSLabLike>(other_color);
return m_properties == other_lab_like.m_properties;
}
Color CSSOKLab::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);
@ -23,17 +34,6 @@ Color CSSOKLab::to_color(Optional<Layout::NodeWithStyle const&>) const
return Color::from_oklab(l_val, a_val, b_val, alpha_val);
}
bool CSSOKLab::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_oklab = verify_cast<CSSOKLab>(other_color);
return m_properties == other_oklab.m_properties;
}
// https://www.w3.org/TR/css-color-4/#serializing-oklab-oklch
String CSSOKLab::to_string() const
{

View file

@ -11,33 +11,20 @@
namespace Web::CSS {
// https://drafts.css-houdini.org/css-typed-om-1/#cssoklab
class CSSOKLab final : public CSSColorValue {
class CSSLabLike : public CSSColorValue {
public:
static ValueComparingNonnullRefPtr<CSSOKLab> create(ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingNonnullRefPtr<CSSStyleValue> a, ValueComparingNonnullRefPtr<CSSStyleValue> b, ValueComparingRefPtr<CSSStyleValue> alpha = {})
{
// alpha defaults to 1
if (!alpha)
return adopt_ref(*new (nothrow) CSSOKLab(move(l), move(a), move(b), NumberStyleValue::create(1)));
return adopt_ref(*new (nothrow) CSSOKLab(move(l), move(a), move(b), alpha.release_nonnull()));
}
virtual ~CSSOKLab() override = default;
virtual ~CSSLabLike() override = default;
CSSStyleValue const& l() const { return *m_properties.l; }
CSSStyleValue const& a() const { return *m_properties.a; }
CSSStyleValue const& b() const { return *m_properties.b; }
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:
CSSOKLab(ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingNonnullRefPtr<CSSStyleValue> a, ValueComparingNonnullRefPtr<CSSStyleValue> b, ValueComparingNonnullRefPtr<CSSStyleValue> alpha)
: CSSColorValue(ColorType::OKLab)
protected:
CSSLabLike(ColorType color_type, ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingNonnullRefPtr<CSSStyleValue> a, ValueComparingNonnullRefPtr<CSSStyleValue> b, ValueComparingNonnullRefPtr<CSSStyleValue> alpha)
: CSSColorValue(color_type)
, m_properties { .l = move(l), .a = move(a), .b = move(b), .alpha = move(alpha) }
{
}
@ -51,4 +38,26 @@ private:
} m_properties;
};
// https://drafts.css-houdini.org/css-typed-om-1/#cssoklab
class CSSOKLab final : public CSSLabLike {
public:
static ValueComparingNonnullRefPtr<CSSOKLab> create(ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingNonnullRefPtr<CSSStyleValue> a, ValueComparingNonnullRefPtr<CSSStyleValue> b, ValueComparingRefPtr<CSSStyleValue> alpha = {})
{
// alpha defaults to 1
if (!alpha)
return adopt_ref(*new (nothrow) CSSOKLab(move(l), move(a), move(b), NumberStyleValue::create(1)));
return adopt_ref(*new (nothrow) CSSOKLab(move(l), move(a), move(b), alpha.release_nonnull()));
}
virtual Color to_color(Optional<Layout::NodeWithStyle const&>) const override;
virtual String to_string() const override;
private:
CSSOKLab(ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingNonnullRefPtr<CSSStyleValue> a, ValueComparingNonnullRefPtr<CSSStyleValue> b, ValueComparingNonnullRefPtr<CSSStyleValue> alpha)
: CSSLabLike(ColorType::OKLab, move(l), move(a), move(b), move(alpha))
{
}
};
}