/src/serenity/Userland/Libraries/LibWeb/CSS/StyleValues/CSSLabLike.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2024, Sam Atkins <sam@ladybird.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include "CSSLabLike.h" |
8 | | #include <AK/TypeCasts.h> |
9 | | #include <LibWeb/CSS/Serialize.h> |
10 | | #include <LibWeb/CSS/StyleValues/CSSMathValue.h> |
11 | | #include <LibWeb/CSS/StyleValues/NumberStyleValue.h> |
12 | | #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h> |
13 | | |
14 | | namespace Web::CSS { |
15 | | |
16 | | bool CSSLabLike::equals(CSSStyleValue const& other) const |
17 | 0 | { |
18 | 0 | if (type() != other.type()) |
19 | 0 | return false; |
20 | 0 | auto const& other_color = other.as_color(); |
21 | 0 | if (color_type() != other_color.color_type()) |
22 | 0 | return false; |
23 | 0 | auto const& other_lab_like = verify_cast<CSSLabLike>(other_color); |
24 | 0 | return m_properties == other_lab_like.m_properties; |
25 | 0 | } |
26 | | |
27 | | Color CSSOKLab::to_color(Optional<Layout::NodeWithStyle const&>) const |
28 | 0 | { |
29 | 0 | auto const l_val = clamp(resolve_with_reference_value(m_properties.l, 1.0).value_or(0), 0, 1); |
30 | 0 | auto const a_val = resolve_with_reference_value(m_properties.a, 0.4).value_or(0); |
31 | 0 | auto const b_val = resolve_with_reference_value(m_properties.b, 0.4).value_or(0); |
32 | 0 | auto const alpha_val = resolve_alpha(m_properties.alpha).value_or(1); |
33 | |
|
34 | 0 | return Color::from_oklab(l_val, a_val, b_val, alpha_val); |
35 | 0 | } |
36 | | |
37 | | // https://www.w3.org/TR/css-color-4/#serializing-oklab-oklch |
38 | | String CSSOKLab::to_string() const |
39 | 0 | { |
40 | | // FIXME: Do this properly, taking unresolved calculated values into account. |
41 | 0 | return serialize_a_srgb_value(to_color({})); |
42 | 0 | } |
43 | | |
44 | | Color CSSLab::to_color(Optional<Layout::NodeWithStyle const&>) const |
45 | 0 | { |
46 | 0 | auto const l_val = clamp(resolve_with_reference_value(m_properties.l, 100).value_or(0), 0, 100); |
47 | 0 | auto const a_val = resolve_with_reference_value(m_properties.a, 125).value_or(0); |
48 | 0 | auto const b_val = resolve_with_reference_value(m_properties.b, 125).value_or(0); |
49 | 0 | auto const alpha_val = resolve_alpha(m_properties.alpha).value_or(1); |
50 | |
|
51 | 0 | return Color::from_lab(l_val, a_val, b_val, alpha_val); |
52 | 0 | } |
53 | | |
54 | | // https://www.w3.org/TR/css-color-4/#serializing-lab-lch |
55 | | String CSSLab::to_string() const |
56 | 0 | { |
57 | | // FIXME: Do this properly, taking unresolved calculated values into account. |
58 | 0 | return serialize_a_srgb_value(to_color({})); |
59 | 0 | } |
60 | | |
61 | | } |