/src/serenity/Userland/Libraries/LibWeb/CSS/StyleValues/CSSRGB.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2024, Sam Atkins <sam@ladybird.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <LibWeb/CSS/StyleValues/CSSColorValue.h> |
10 | | #include <LibWeb/CSS/StyleValues/NumberStyleValue.h> |
11 | | |
12 | | namespace Web::CSS { |
13 | | |
14 | | // https://drafts.css-houdini.org/css-typed-om-1/#cssrgb |
15 | | class CSSRGB final : public CSSColorValue { |
16 | | public: |
17 | | static ValueComparingNonnullRefPtr<CSSRGB> create(ValueComparingNonnullRefPtr<CSSStyleValue> r, ValueComparingNonnullRefPtr<CSSStyleValue> g, ValueComparingNonnullRefPtr<CSSStyleValue> b, ValueComparingRefPtr<CSSStyleValue> alpha = {}) |
18 | 0 | { |
19 | | // alpha defaults to 1 |
20 | 0 | if (!alpha) |
21 | 0 | return adopt_ref(*new (nothrow) CSSRGB(move(r), move(g), move(b), NumberStyleValue::create(1))); |
22 | | |
23 | 0 | return adopt_ref(*new (nothrow) CSSRGB(move(r), move(g), move(b), alpha.release_nonnull())); |
24 | 0 | } |
25 | 0 | virtual ~CSSRGB() override = default; |
26 | | |
27 | 0 | CSSStyleValue const& r() const { return *m_properties.r; } |
28 | 0 | CSSStyleValue const& g() const { return *m_properties.g; } |
29 | 0 | CSSStyleValue const& b() const { return *m_properties.b; } |
30 | 0 | CSSStyleValue const& alpha() const { return *m_properties.alpha; } |
31 | | |
32 | | virtual Color to_color(Optional<Layout::NodeWithStyle const&>) const override; |
33 | | |
34 | | String to_string() const override; |
35 | | |
36 | | virtual bool equals(CSSStyleValue const& other) const override; |
37 | | |
38 | | private: |
39 | | CSSRGB(ValueComparingNonnullRefPtr<CSSStyleValue> r, ValueComparingNonnullRefPtr<CSSStyleValue> g, ValueComparingNonnullRefPtr<CSSStyleValue> b, ValueComparingNonnullRefPtr<CSSStyleValue> alpha) |
40 | 0 | : CSSColorValue(ColorType::RGB) |
41 | 0 | , m_properties { .r = move(r), .g = move(g), .b = move(b), .alpha = move(alpha) } |
42 | 0 | { |
43 | 0 | } |
44 | | |
45 | | struct Properties { |
46 | | ValueComparingNonnullRefPtr<CSSStyleValue> r; |
47 | | ValueComparingNonnullRefPtr<CSSStyleValue> g; |
48 | | ValueComparingNonnullRefPtr<CSSStyleValue> b; |
49 | | ValueComparingNonnullRefPtr<CSSStyleValue> alpha; |
50 | 0 | bool operator==(Properties const&) const = default; |
51 | | } m_properties; |
52 | | }; |
53 | | |
54 | | } |