CSSColorValue.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 <AK/FlyString.h>
  11. #include <LibGfx/Color.h>
  12. #include <LibWeb/CSS/CSSStyleValue.h>
  13. namespace Web::CSS {
  14. // https://drafts.css-houdini.org/css-typed-om-1/#csscolorvalue
  15. class CSSColorValue : public CSSStyleValue {
  16. public:
  17. static ValueComparingNonnullRefPtr<CSSColorValue> create_from_color(Color color, Optional<FlyString> name = {});
  18. virtual ~CSSColorValue() override = default;
  19. virtual bool has_color() const override { return true; }
  20. enum class ColorType {
  21. RGB, // This is used by CSSRGB for rgb(...) and rgba(...).
  22. A98RGB,
  23. DisplayP3,
  24. HSL,
  25. HWB,
  26. Lab,
  27. LCH,
  28. OKLab,
  29. OKLCH,
  30. sRGB, // This is used by CSSColor for color(srgb ...).
  31. sRGBLinear,
  32. ProPhotoRGB,
  33. Rec2020,
  34. XYZD50,
  35. XYZD65,
  36. };
  37. ColorType color_type() const { return m_color_type; }
  38. protected:
  39. explicit CSSColorValue(ColorType color_type)
  40. : CSSStyleValue(Type::Color)
  41. , m_color_type(color_type)
  42. {
  43. }
  44. static Optional<double> resolve_hue(CSSStyleValue const&);
  45. static Optional<double> resolve_with_reference_value(CSSStyleValue const&, float one_hundred_percent_value);
  46. static Optional<double> resolve_alpha(CSSStyleValue const&);
  47. ColorType m_color_type;
  48. };
  49. }