CanvasGradient.cpp 2.8 KB

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