CanvasGradient.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.h>
  7. #include <LibWeb/HTML/CanvasGradient.h>
  8. #include <LibWeb/HTML/Window.h>
  9. #include <LibWeb/WebIDL/ExceptionOr.h>
  10. namespace Web::HTML {
  11. JS::NonnullGCPtr<CanvasGradient> CanvasGradient::create_radial(HTML::Window& window, double x0, double y0, double r0, double x1, double y1, double r1)
  12. {
  13. (void)x0;
  14. (void)y0;
  15. (void)r0;
  16. (void)x1;
  17. (void)y1;
  18. (void)r1;
  19. return *window.heap().allocate<CanvasGradient>(window.realm(), window, Type::Radial);
  20. }
  21. JS::NonnullGCPtr<CanvasGradient> CanvasGradient::create_linear(HTML::Window& window, double x0, double y0, double x1, double y1)
  22. {
  23. (void)x0;
  24. (void)y0;
  25. (void)x1;
  26. (void)y1;
  27. return *window.heap().allocate<CanvasGradient>(window.realm(), window, Type::Linear);
  28. }
  29. JS::NonnullGCPtr<CanvasGradient> CanvasGradient::create_conic(HTML::Window& window, double start_angle, double x, double y)
  30. {
  31. (void)start_angle;
  32. (void)x;
  33. (void)y;
  34. return *window.heap().allocate<CanvasGradient>(window.realm(), window, Type::Conic);
  35. }
  36. CanvasGradient::CanvasGradient(HTML::Window& window, Type type)
  37. : PlatformObject(window.realm())
  38. , m_type(type)
  39. {
  40. set_prototype(&window.cached_web_prototype("CanvasGradient"));
  41. }
  42. CanvasGradient::~CanvasGradient() = default;
  43. // https://html.spec.whatwg.org/multipage/canvas.html#dom-canvasgradient-addcolorstop
  44. WebIDL::ExceptionOr<void> CanvasGradient::add_color_stop(double offset, String const& color)
  45. {
  46. // 1. If the offset is less than 0 or greater than 1, then throw an "IndexSizeError" DOMException.
  47. if (offset < 0 || offset > 1)
  48. return DOM::IndexSizeError::create(global_object(), "CanvasGradient color stop offset out of bounds");
  49. // 2. Let parsed color be the result of parsing color.
  50. auto parsed_color = Color::from_string(color);
  51. // 3. If parsed color is failure, throw a "SyntaxError" DOMException.
  52. if (!parsed_color.has_value())
  53. return DOM::SyntaxError::create(global_object(), "Could not parse color for CanvasGradient");
  54. // 4. Place a new stop on the gradient, at offset offset relative to the whole gradient, and with the color parsed color.
  55. m_color_stops.append(ColorStop { offset, parsed_color.value() });
  56. // FIXME: If multiple stops are added at the same offset on a gradient, then they must be placed in the order added,
  57. // with the first one closest to the start of the gradient, and each subsequent one infinitesimally further along
  58. // towards the end point (in effect causing all but the first and last stop added at each point to be ignored).
  59. quick_sort(m_color_stops, [](auto& a, auto& b) { return a.offset < b.offset; });
  60. return {};
  61. }
  62. }