CSSLCHLike.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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<CSSOKLCH> 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/#cssoklch
  41. class CSSOKLCH final : public CSSLCHLike {
  42. public:
  43. CSSOKLCH(Badge<CSSLCHLike>, ValueComparingNonnullRefPtr<CSSStyleValue> l, ValueComparingNonnullRefPtr<CSSStyleValue> c, ValueComparingNonnullRefPtr<CSSStyleValue> h, ValueComparingNonnullRefPtr<CSSStyleValue> alpha)
  44. : CSSLCHLike(ColorType::OKLCH, move(l), move(c), move(h), move(alpha))
  45. {
  46. }
  47. virtual ~CSSOKLCH() override = default;
  48. virtual Color to_color(Optional<Layout::NodeWithStyle const&>) const override;
  49. String to_string() const override;
  50. };
  51. }