/src/serenity/Userland/Libraries/LibWeb/CSS/StyleValues/CSSLCHLike.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 "CSSLCHLike.h" |
8 | | #include <AK/Math.h> |
9 | | #include <AK/TypeCasts.h> |
10 | | #include <LibWeb/CSS/Serialize.h> |
11 | | #include <LibWeb/CSS/StyleValues/CSSMathValue.h> |
12 | | #include <LibWeb/CSS/StyleValues/NumberStyleValue.h> |
13 | | #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h> |
14 | | |
15 | | namespace Web::CSS { |
16 | | |
17 | | bool CSSLCHLike::equals(CSSStyleValue const& other) const |
18 | 0 | { |
19 | 0 | if (type() != other.type()) |
20 | 0 | return false; |
21 | 0 | auto const& other_color = other.as_color(); |
22 | 0 | if (color_type() != other_color.color_type()) |
23 | 0 | return false; |
24 | 0 | auto const& other_oklch_like = verify_cast<CSSLCHLike>(other_color); |
25 | 0 | return m_properties == other_oklch_like.m_properties; |
26 | 0 | } |
27 | | |
28 | | Color CSSLCH::to_color(Optional<Layout::NodeWithStyle const&>) const |
29 | 0 | { |
30 | 0 | auto const l_val = clamp(resolve_with_reference_value(m_properties.l, 100).value_or(0), 0, 100); |
31 | 0 | auto const c_val = resolve_with_reference_value(m_properties.c, 150).value_or(0); |
32 | 0 | auto const h_val = AK::to_radians(resolve_hue(m_properties.h).value_or(0)); |
33 | 0 | auto const alpha_val = resolve_alpha(m_properties.alpha).value_or(1); |
34 | |
|
35 | 0 | return Color::from_lab(l_val, c_val * cos(h_val), c_val * sin(h_val), alpha_val); |
36 | 0 | } |
37 | | |
38 | | // https://www.w3.org/TR/css-color-4/#serializing-lab-lch |
39 | | String CSSLCH::to_string() const |
40 | 0 | { |
41 | | // FIXME: Do this properly, taking unresolved calculated values into account. |
42 | 0 | return serialize_a_srgb_value(to_color({})); |
43 | 0 | } |
44 | | |
45 | | Color CSSOKLCH::to_color(Optional<Layout::NodeWithStyle const&>) const |
46 | 0 | { |
47 | 0 | auto const l_val = clamp(resolve_with_reference_value(m_properties.l, 1.0).value_or(0), 0, 1); |
48 | 0 | auto const c_val = max(resolve_with_reference_value(m_properties.c, 0.4).value_or(0), 0); |
49 | 0 | auto const h_val = AK::to_radians(resolve_hue(m_properties.h).value_or(0)); |
50 | 0 | auto const alpha_val = resolve_alpha(m_properties.alpha).value_or(1); |
51 | |
|
52 | 0 | return Color::from_oklab(l_val, c_val * cos(h_val), c_val * sin(h_val), alpha_val); |
53 | 0 | } |
54 | | |
55 | | // https://www.w3.org/TR/css-color-4/#serializing-oklab-oklch |
56 | | String CSSOKLCH::to_string() const |
57 | 0 | { |
58 | | // FIXME: Do this properly, taking unresolved calculated values into account. |
59 | 0 | return serialize_a_srgb_value(to_color({})); |
60 | 0 | } |
61 | | |
62 | | } |