CSSColorValue.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #pragma once
  10. #include <LibGfx/Color.h>
  11. #include <LibWeb/CSS/CSSStyleValue.h>
  12. namespace Web::CSS {
  13. // https://drafts.css-houdini.org/css-typed-om-1/#csscolorvalue
  14. class CSSColorValue : public CSSStyleValue {
  15. public:
  16. static ValueComparingNonnullRefPtr<CSSColorValue> create_from_color(Color color);
  17. virtual ~CSSColorValue() override = default;
  18. virtual bool has_color() const override { return true; }
  19. enum class ColorType {
  20. RGB, // This is used by CSSRGB for rgb(...) and rgba(...).
  21. HSL,
  22. HWB,
  23. Lab,
  24. LCH,
  25. OKLab,
  26. OKLCH,
  27. sRGB, // This is used by CSSColor for color(srgb ...).
  28. XYZD50,
  29. XYZD65,
  30. };
  31. ColorType color_type() const { return m_color_type; }
  32. protected:
  33. explicit CSSColorValue(ColorType color_type)
  34. : CSSStyleValue(Type::Color)
  35. , m_color_type(color_type)
  36. {
  37. }
  38. static Optional<float> resolve_hue(CSSStyleValue const&);
  39. static Optional<float> resolve_with_reference_value(CSSStyleValue const&, float one_hundred_percent_value);
  40. static Optional<float> resolve_alpha(CSSStyleValue const&);
  41. private:
  42. ColorType m_color_type;
  43. };
  44. }