/src/serenity/Userland/Libraries/LibWeb/CSS/StyleValues/CSSRGB.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2024, Sam Atkins <sam@ladybird.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include "CSSRGB.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 | | Color CSSRGB::to_color(Optional<Layout::NodeWithStyle const&>) const |
17 | 0 | { |
18 | 0 | auto resolve_rgb_to_u8 = [](CSSStyleValue const& style_value) -> Optional<u8> { |
19 | | // <number> | <percentage> | none |
20 | 0 | auto normalized = [](double number) { |
21 | 0 | return llround(clamp(number, 0.0, 255.0)); |
22 | 0 | }; |
23 | |
|
24 | 0 | if (style_value.is_number()) |
25 | 0 | return normalized(style_value.as_number().number()); |
26 | | |
27 | 0 | if (style_value.is_percentage()) |
28 | 0 | return normalized(style_value.as_percentage().value() * 255 / 100); |
29 | | |
30 | 0 | if (style_value.is_math()) { |
31 | 0 | auto const& calculated = style_value.as_math(); |
32 | 0 | if (calculated.resolves_to_number()) |
33 | 0 | return normalized(calculated.resolve_number().value()); |
34 | 0 | if (calculated.resolves_to_percentage()) |
35 | 0 | return normalized(calculated.resolve_percentage().value().value() * 255 / 100); |
36 | 0 | } |
37 | | |
38 | 0 | if (style_value.is_keyword() && style_value.to_keyword() == Keyword::None) |
39 | 0 | return 0; |
40 | | |
41 | 0 | return {}; |
42 | 0 | }; |
43 | |
|
44 | 0 | auto resolve_alpha_to_u8 = [](CSSStyleValue const& style_value) -> Optional<u8> { |
45 | 0 | auto alpha_0_1 = resolve_alpha(style_value); |
46 | 0 | if (alpha_0_1.has_value()) |
47 | 0 | return llround(clamp(alpha_0_1.value() * 255.0f, 0.0f, 255.0f)); |
48 | 0 | return {}; |
49 | 0 | }; |
50 | |
|
51 | 0 | u8 const r_val = resolve_rgb_to_u8(m_properties.r).value_or(0); |
52 | 0 | u8 const g_val = resolve_rgb_to_u8(m_properties.g).value_or(0); |
53 | 0 | u8 const b_val = resolve_rgb_to_u8(m_properties.b).value_or(0); |
54 | 0 | u8 const alpha_val = resolve_alpha_to_u8(m_properties.alpha).value_or(255); |
55 | |
|
56 | 0 | return Color(r_val, g_val, b_val, alpha_val); |
57 | 0 | } |
58 | | |
59 | | bool CSSRGB::equals(CSSStyleValue const& other) const |
60 | 0 | { |
61 | 0 | if (type() != other.type()) |
62 | 0 | return false; |
63 | 0 | auto const& other_color = other.as_color(); |
64 | 0 | if (color_type() != other_color.color_type()) |
65 | 0 | return false; |
66 | 0 | auto const& other_rgb = verify_cast<CSSRGB>(other_color); |
67 | 0 | return m_properties == other_rgb.m_properties; |
68 | 0 | } |
69 | | |
70 | | // https://www.w3.org/TR/css-color-4/#serializing-sRGB-values |
71 | | String CSSRGB::to_string() const |
72 | 0 | { |
73 | | // FIXME: Do this properly, taking unresolved calculated values into account. |
74 | 0 | return serialize_a_srgb_value(to_color({})); |
75 | 0 | } |
76 | | |
77 | | } |