CSSColorValue.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. A98RGB,
  22. HSL,
  23. HWB,
  24. Lab,
  25. LCH,
  26. OKLab,
  27. OKLCH,
  28. sRGB, // This is used by CSSColor for color(srgb ...).
  29. sRGBLinear,
  30. XYZD50,
  31. XYZD65,
  32. };
  33. ColorType color_type() const { return m_color_type; }
  34. protected:
  35. explicit CSSColorValue(ColorType color_type)
  36. : CSSStyleValue(Type::Color)
  37. , m_color_type(color_type)
  38. {
  39. }
  40. static Optional<float> resolve_hue(CSSStyleValue const&);
  41. static Optional<float> resolve_with_reference_value(CSSStyleValue const&, float one_hundred_percent_value);
  42. static Optional<float> resolve_alpha(CSSStyleValue const&);
  43. private:
  44. ColorType m_color_type;
  45. };
  46. }