From b2a0d70ef3b9e8320be213461fee14d8397dc266 Mon Sep 17 00:00:00 2001 From: MacDue Date: Sat, 5 Nov 2022 21:44:58 +0000 Subject: [PATCH] LibWeb: Fix some `conic-gradient()` painting issues This makes the center position the center of the pixel rather than the top left corner (which fixes some small artifacts on a few gradients). This also now floors the angle used to sample from the gradient line, this avoids the colors diverging the further away from the center you get (which is noticeable on hard-edge gradients). --- Userland/Libraries/LibWeb/Painting/GradientPainting.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWeb/Painting/GradientPainting.cpp b/Userland/Libraries/LibWeb/Painting/GradientPainting.cpp index c049eec6f7..02fa27fedb 100644 --- a/Userland/Libraries/LibWeb/Painting/GradientPainting.cpp +++ b/Userland/Libraries/LibWeb/Painting/GradientPainting.cpp @@ -273,10 +273,13 @@ void paint_conic_gradient(PaintContext& context, Gfx::IntRect const& gradient_re // FIXME: Do we need/want sub-degree accuracy for the gradient line? GradientLine gradient_line(360, data.color_stops); float start_angle = (360.0f - data.start_angle) + 90.0f; + // Translate position/center to the center of the pixel (avoids some funky painting) + auto center_point = Gfx::FloatPoint { position }.translated(0.5, 0.5); gradient_line.paint_into_rect(context.painter(), gradient_rect, [&](int x, int y) { - auto point = Gfx::IntPoint { x, y } - position; + auto point = Gfx::FloatPoint { x, y } - center_point; // FIXME: We could probably get away with some approximation here: - return fmod((AK::atan2(float(point.y()), float(point.x())) * 180.0f / AK::Pi + 360.0f + start_angle), 360.0f); + // Note: We need too floor the angle here or the colors will start to diverge as you get further from the center. + return floor(fmod((AK::atan2(point.y(), point.x()) * 180.0f / AK::Pi + 360.0f + start_angle), 360.0f)); }); }