CSSLCHLike.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. class CSSLCHLike : public CSSColorValue {
  11. public:
  12. template<DerivedFrom<CSSLCHLike> T>
  13. static ValueComparingNonnullRefPtr<T> create(ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingNonnullRefPtr<CSSStyleValue> c, ValueComparingNonnullRefPtr<CSSStyleValue> h, ValueComparingRefPtr<CSSStyleValue> alpha = {})
  14. {
  15. // alpha defaults to 1
  16. if (!alpha)
  17. alpha = NumberStyleValue::create(1);
  18. return adopt_ref(*new (nothrow) T({}, move(l), move(c), move(h), alpha.release_nonnull()));
  19. }
  20. virtual ~CSSLCHLike() override = default;
  21. CSSStyleValue const& l() const { return *m_properties.l; }
  22. CSSStyleValue const& c() const { return *m_properties.c; }
  23. CSSStyleValue const& h() const { return *m_properties.h; }
  24. CSSStyleValue const& alpha() const { return *m_properties.alpha; }
  25. virtual bool equals(CSSStyleValue const& other) const override;
  26. protected:
  27. CSSLCHLike(ColorType color_type, ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingNonnullRefPtr<CSSStyleValue> c, ValueComparingNonnullRefPtr<CSSStyleValue> h, ValueComparingNonnullRefPtr<CSSStyleValue> alpha)
  28. : CSSColorValue(color_type)
  29. , m_properties { .l = move(l), .c = move(c), .h = move(h), .alpha = move(alpha) }
  30. {
  31. }
  32. struct Properties {
  33. ValueComparingNonnullRefPtr<CSSStyleValue> l;
  34. ValueComparingNonnullRefPtr<CSSStyleValue> c;
  35. ValueComparingNonnullRefPtr<CSSStyleValue> h;
  36. ValueComparingNonnullRefPtr<CSSStyleValue> alpha;
  37. bool operator==(Properties const&) const = default;
  38. } m_properties;
  39. };
  40. // https://drafts.css-houdini.org/css-typed-om-1/#csslch
  41. class CSSLCH final : public CSSLCHLike {
  42. public:
  43. CSSLCH(Badge<CSSLCHLike>, ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingNonnullRefPtr<CSSStyleValue> c, ValueComparingNonnullRefPtr<CSSStyleValue> h, ValueComparingNonnullRefPtr<CSSStyleValue> alpha)
  44. : CSSLCHLike(ColorType::LCH, move(l), move(c), move(h), move(alpha))
  45. {
  46. }
  47. virtual ~CSSLCH() override = default;
  48. virtual Color to_color(Optional<Layout::NodeWithStyle const&>) const override;
  49. virtual String to_string() const override;
  50. };
  51. // https://drafts.css-houdini.org/css-typed-om-1/#cssoklch
  52. class CSSOKLCH final : public CSSLCHLike {
  53. public:
  54. CSSOKLCH(Badge<CSSLCHLike>, ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingNonnullRefPtr<CSSStyleValue> c, ValueComparingNonnullRefPtr<CSSStyleValue> h, ValueComparingNonnullRefPtr<CSSStyleValue> alpha)
  55. : CSSLCHLike(ColorType::OKLCH, move(l), move(c), move(h), move(alpha))
  56. {
  57. }
  58. virtual ~CSSOKLCH() override = default;
  59. virtual Color to_color(Optional<Layout::NodeWithStyle const&>) const override;
  60. String to_string() const override;
  61. };
  62. }