RadialGradientStyleValue.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.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/Vector.h>
  11. #include <LibWeb/CSS/Enums.h>
  12. #include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
  13. namespace Web::CSS {
  14. class RadialGradientStyleValue final : public AbstractImageStyleValue {
  15. public:
  16. enum class EndingShape {
  17. Circle,
  18. Ellipse
  19. };
  20. enum class Extent {
  21. ClosestCorner,
  22. ClosestSide,
  23. FarthestCorner,
  24. FarthestSide
  25. };
  26. struct CircleSize {
  27. Length radius;
  28. bool operator==(CircleSize const&) const = default;
  29. };
  30. struct EllipseSize {
  31. LengthPercentage radius_a;
  32. LengthPercentage radius_b;
  33. bool operator==(EllipseSize const&) const = default;
  34. };
  35. using Size = Variant<Extent, CircleSize, EllipseSize>;
  36. static ValueComparingNonnullRefPtr<RadialGradientStyleValue> create(EndingShape ending_shape, Size size, PositionValue position, Vector<LinearColorStopListElement> color_stop_list, GradientRepeating repeating)
  37. {
  38. VERIFY(color_stop_list.size() >= 2);
  39. return adopt_ref(*new RadialGradientStyleValue(ending_shape, size, position, move(color_stop_list), repeating));
  40. }
  41. virtual ErrorOr<String> to_string() const override;
  42. void paint(PaintContext&, DevicePixelRect const& dest_rect, CSS::ImageRendering) const override;
  43. virtual bool equals(StyleValue const& other) const override;
  44. Vector<LinearColorStopListElement> const& color_stop_list() const
  45. {
  46. return m_properties.color_stop_list;
  47. }
  48. bool is_paintable() const override { return true; }
  49. void resolve_for_size(Layout::Node const&, CSSPixelSize) const override;
  50. Gfx::FloatSize resolve_size(Layout::Node const&, Gfx::FloatPoint, Gfx::FloatRect const&) const;
  51. bool is_repeating() const { return m_properties.repeating == GradientRepeating::Yes; }
  52. virtual ~RadialGradientStyleValue() override = default;
  53. private:
  54. RadialGradientStyleValue(EndingShape ending_shape, Size size, PositionValue position, Vector<LinearColorStopListElement> color_stop_list, GradientRepeating repeating)
  55. : AbstractImageStyleValue(Type::RadialGradient)
  56. , m_properties { .ending_shape = ending_shape, .size = size, .position = position, .color_stop_list = move(color_stop_list), .repeating = repeating }
  57. {
  58. }
  59. struct Properties {
  60. EndingShape ending_shape;
  61. Size size;
  62. PositionValue position;
  63. Vector<LinearColorStopListElement> color_stop_list;
  64. GradientRepeating repeating;
  65. bool operator==(Properties const&) const = default;
  66. } m_properties;
  67. struct ResolvedData {
  68. Painting::RadialGradientData data;
  69. Gfx::FloatSize gradient_size;
  70. Gfx::FloatPoint center;
  71. };
  72. mutable Optional<ResolvedData> m_resolved;
  73. };
  74. }