LibWeb/CSS: Implement the light-dark color function

This commit is contained in:
Gingeh 2025-01-05 09:51:58 +11:00 committed by Sam Atkins
commit 8e56109515
Notes: github-actions[bot] 2025-01-08 11:19:19 +00:00
14 changed files with 275 additions and 0 deletions

View file

@ -39,6 +39,7 @@ public:
Rec2020,
XYZD50,
XYZD65,
LightDark, // This is used by CSSLightDark for light-dark(..., ...).
};
ColorType color_type() const { return m_color_type; }

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2025, Ladybird contributors
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "CSSLightDark.h"
#include <LibWeb/Layout/Node.h>
namespace Web::CSS {
Color CSSLightDark::to_color(Optional<Layout::NodeWithStyle const&> node) const
{
if (node.has_value() && node.value().computed_values().color_scheme() == PreferredColorScheme::Dark)
return m_properties.dark->to_color(node);
return m_properties.light->to_color(node);
}
bool CSSLightDark::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_light_dark = verify_cast<CSSLightDark>(other_color);
return m_properties == other_light_dark.m_properties;
}
String CSSLightDark::to_string(SerializationMode mode) const
{
// FIXME: We don't have enough information to determine the computed value here.
return MUST(String::formatted("light-dark({}, {})", m_properties.light->to_string(mode), m_properties.dark->to_string(mode)));
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2025, Ladybird contributors
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/CSS/StyleValues/CSSColorValue.h>
namespace Web::CSS {
// https://drafts.csswg.org/css-color-5/#funcdef-light-dark
class CSSLightDark final : public CSSColorValue {
public:
virtual ~CSSLightDark() override = default;
static ValueComparingNonnullRefPtr<CSSLightDark> create(ValueComparingNonnullRefPtr<CSSStyleValue> light, ValueComparingNonnullRefPtr<CSSStyleValue> dark)
{
return AK::adopt_ref(*new (nothrow) CSSLightDark(move(light), move(dark)));
}
virtual bool equals(CSSStyleValue const&) const override;
virtual Color to_color(Optional<Layout::NodeWithStyle const&>) const override;
virtual String to_string(SerializationMode) const override;
private:
CSSLightDark(ValueComparingNonnullRefPtr<CSSStyleValue> light, ValueComparingNonnullRefPtr<CSSStyleValue> dark)
: CSSColorValue(CSSColorValue::ColorType::LightDark)
, m_properties { .light = move(light), .dark = move(dark) }
{
}
struct Properties {
ValueComparingNonnullRefPtr<CSSStyleValue> light;
ValueComparingNonnullRefPtr<CSSStyleValue> dark;
bool operator==(Properties const&) const = default;
};
Properties m_properties;
};
} // Web::CSS