CSSHSL.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/CSS/StyleValues/CSSColorValue.h>
  8. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  9. namespace Web::CSS {
  10. // https://drafts.css-houdini.org/css-typed-om-1/#csshsl
  11. class CSSHSL final : public CSSColorValue {
  12. public:
  13. static ValueComparingNonnullRefPtr<CSSHSL> create(ValueComparingNonnullRefPtr<CSSStyleValue> h, ValueComparingNonnullRefPtr<CSSStyleValue> s, ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingRefPtr<CSSStyleValue> alpha = {})
  14. {
  15. // alpha defaults to 1
  16. if (!alpha)
  17. return adopt_ref(*new (nothrow) CSSHSL(move(h), move(s), move(l), NumberStyleValue::create(1)));
  18. return adopt_ref(*new (nothrow) CSSHSL(move(h), move(s), move(l), alpha.release_nonnull()));
  19. }
  20. virtual ~CSSHSL() override = default;
  21. CSSStyleValue const& h() const { return *m_properties.h; }
  22. CSSStyleValue const& s() const { return *m_properties.s; }
  23. CSSStyleValue const& l() const { return *m_properties.l; }
  24. CSSStyleValue const& alpha() const { return *m_properties.alpha; }
  25. virtual Color to_color(Optional<Layout::NodeWithStyle const&>) const override;
  26. String to_string() const override;
  27. virtual bool equals(CSSStyleValue const& other) const override;
  28. private:
  29. CSSHSL(ValueComparingNonnullRefPtr<CSSStyleValue> h, ValueComparingNonnullRefPtr<CSSStyleValue> s, ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingNonnullRefPtr<CSSStyleValue> alpha)
  30. : CSSColorValue(ColorType::HSL)
  31. , m_properties { .h = move(h), .s = move(s), .l = move(l), .alpha = move(alpha) }
  32. {
  33. }
  34. struct Properties {
  35. ValueComparingNonnullRefPtr<CSSStyleValue> h;
  36. ValueComparingNonnullRefPtr<CSSStyleValue> s;
  37. ValueComparingNonnullRefPtr<CSSStyleValue> l;
  38. ValueComparingNonnullRefPtr<CSSStyleValue> alpha;
  39. bool operator==(Properties const&) const = default;
  40. } m_properties;
  41. };
  42. }