From e6f279dadaebbdb032a8f45a34dbafeb48a593c4 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 5 Feb 2022 15:16:35 +0100 Subject: [PATCH] LibWeb: Implement CanvasGradient.addColorStop() according to spec The object is still not usable for anything, but at least now it behaves correctly with regards to throwing exceptions. --- .../Libraries/LibWeb/HTML/CanvasGradient.cpp | 26 +++++++++++++++++-- .../Libraries/LibWeb/HTML/CanvasGradient.h | 10 ++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/CanvasGradient.cpp b/Userland/Libraries/LibWeb/HTML/CanvasGradient.cpp index 91673b6150..830a6533c8 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasGradient.cpp +++ b/Userland/Libraries/LibWeb/HTML/CanvasGradient.cpp @@ -4,6 +4,8 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include +#include #include namespace Web::HTML { @@ -45,9 +47,29 @@ CanvasGradient::~CanvasGradient() { } -void CanvasGradient::add_color_stop(double offset, String const& color) +// https://html.spec.whatwg.org/multipage/canvas.html#dom-canvasgradient-addcolorstop +DOM::ExceptionOr CanvasGradient::add_color_stop(double offset, String const& color) { - dbgln("CanvasGradient#addColorStop({}, '{}')", offset, color); + // 1. If the offset is less than 0 or greater than 1, then throw an "IndexSizeError" DOMException. + if (offset < 0 || offset > 1) + return DOM::IndexSizeError::create("CanvasGradient color stop offset out of bounds"); + + // 2. Let parsed color be the result of parsing color. + auto parsed_color = Color::from_string(color); + + // 3. If parsed color is failure, throw a "SyntaxError" DOMException. + if (!parsed_color.has_value()) + return DOM::SyntaxError::create("Could not parse color for CanvasGradient"); + + // 4. Place a new stop on the gradient, at offset offset relative to the whole gradient, and with the color parsed color. + m_color_stops.append(ColorStop { offset, parsed_color.value() }); + + // FIXME: If multiple stops are added at the same offset on a gradient, then they must be placed in the order added, + // with the first one closest to the start of the gradient, and each subsequent one infinitesimally further along + // towards the end point (in effect causing all but the first and last stop added at each point to be ignored). + quick_sort(m_color_stops, [](auto& a, auto& b) { return a.offset < b.offset; }); + + return {}; } } diff --git a/Userland/Libraries/LibWeb/HTML/CanvasGradient.h b/Userland/Libraries/LibWeb/HTML/CanvasGradient.h index 03e1e529d7..d748ba86a7 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasGradient.h +++ b/Userland/Libraries/LibWeb/HTML/CanvasGradient.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include namespace Web::HTML { @@ -27,7 +28,7 @@ public: static NonnullRefPtr create_linear(double x0, double y0, double x1, double y1); static NonnullRefPtr create_conic(double start_angle, double x, double y); - void add_color_stop(double offset, String const& color); + DOM::ExceptionOr add_color_stop(double offset, String const& color); ~CanvasGradient(); @@ -35,6 +36,13 @@ private: explicit CanvasGradient(Type); Type m_type {}; + + struct ColorStop { + double offset { 0 }; + Gfx::Color color; + }; + + Vector m_color_stops; }; }