ConicGradientStyleValue.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #include "ConicGradientStyleValue.h"
  10. #include <LibWeb/CSS/Serialize.h>
  11. namespace Web::CSS {
  12. // FIXME: Temporary until AbstractImageStyleValue.h exists. (And the Serialize.h include above.)
  13. static ErrorOr<void> serialize_color_stop_list(StringBuilder& builder, auto const& color_stop_list)
  14. {
  15. bool first = true;
  16. for (auto const& element : color_stop_list) {
  17. if (!first)
  18. TRY(builder.try_append(", "sv));
  19. if (element.transition_hint.has_value())
  20. TRY(builder.try_appendff("{}, "sv, TRY(element.transition_hint->value.to_string())));
  21. TRY(serialize_a_srgb_value(builder, element.color_stop.color));
  22. for (auto position : Array { &element.color_stop.position, &element.color_stop.second_position }) {
  23. if (position->has_value())
  24. TRY(builder.try_appendff(" {}"sv, TRY((*position)->to_string())));
  25. }
  26. first = false;
  27. }
  28. return {};
  29. }
  30. ErrorOr<String> ConicGradientStyleValue::to_string() const
  31. {
  32. StringBuilder builder;
  33. if (is_repeating())
  34. TRY(builder.try_append("repeating-"sv));
  35. TRY(builder.try_append("conic-gradient("sv));
  36. bool has_from_angle = false;
  37. bool has_at_position = false;
  38. if ((has_from_angle = m_properties.from_angle.to_degrees() != 0))
  39. TRY(builder.try_appendff("from {}", TRY(m_properties.from_angle.to_string())));
  40. if ((has_at_position = m_properties.position != PositionValue::center())) {
  41. if (has_from_angle)
  42. TRY(builder.try_append(' '));
  43. TRY(builder.try_appendff("at "sv));
  44. TRY(m_properties.position.serialize(builder));
  45. }
  46. if (has_from_angle || has_at_position)
  47. TRY(builder.try_append(", "sv));
  48. TRY(serialize_color_stop_list(builder, m_properties.color_stop_list));
  49. TRY(builder.try_append(')'));
  50. return builder.to_string();
  51. }
  52. void ConicGradientStyleValue::resolve_for_size(Layout::Node const& node, CSSPixelSize size) const
  53. {
  54. if (!m_resolved.has_value())
  55. m_resolved = ResolvedData { Painting::resolve_conic_gradient_data(node, *this), {} };
  56. m_resolved->position = m_properties.position.resolved(node, CSSPixelRect { { 0, 0 }, size });
  57. }
  58. void ConicGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
  59. {
  60. VERIFY(m_resolved.has_value());
  61. Painting::paint_conic_gradient(context, dest_rect, m_resolved->data, context.rounded_device_point(m_resolved->position));
  62. }
  63. bool ConicGradientStyleValue::equals(StyleValue const& other) const
  64. {
  65. if (type() != other.type())
  66. return false;
  67. auto& other_gradient = other.as_conic_gradient();
  68. return m_properties == other_gradient.m_properties;
  69. }
  70. float ConicGradientStyleValue::angle_degrees() const
  71. {
  72. return m_properties.from_angle.to_degrees();
  73. }
  74. }