PerKey gradient swatch: align gradient endpoints to visible corners

Cairo linear-gradient endpoints were placed at the rounded rect's
geometric corners (3,3) and (21,21), but the *visible* corner pixel
of a rounded rect with radius 2 is the outermost point of the arc,
inset along the diagonal by r * (1 - 1/sqrt(2)) ≈ 0.586 units.

That meant t=0 and t=1 of the gradient landed in the cut-off corner
regions, and the rendered corners sampled at t≈0.033 / 0.967 — about
3.3% in from each endpoint, ~8 RGB units short of the true previous /
active colors (visible on saturated pairs like pure red → pure blue:
the "pure red" corner rendered as ~rgb(194, 10, 0)).

Shift the gradient endpoints inward by the arc inset so t=0 maps to
the visible TL corner pixel and t=1 to the visible BR pixel. The
gradient now spans the visually rendered area exactly; saturated
endpoints render flush.
This commit is contained in:
Ken Sanislo 2026-05-11 01:13:55 -07:00 committed by Peter F. Patel-Schneider
parent a8e006c948
commit 3dc121e8b3
1 changed files with 7 additions and 1 deletions

View File

@ -211,7 +211,13 @@ class GradientSwatch(Gtk.DrawingArea):
cr.clip()
# Top-left (previous, gradient start) → bottom-right (active, end).
# Matches the directional behavior of dragging the line tool TL → BR.
pat = cairo.LinearGradient(3, 3, 21, 21)
# Endpoints are shifted inward by the arc inset (corner radius * (1
# - 1/sqrt(2)), ~0.586 for r=2) so t=0 lands on the actual visible
# TL corner pixel of the rounded rect — without this, the rendered
# corners sample at t≈0.033/0.967 and the displayed colors are
# ~8 RGB units short of the true endpoint colors.
inset = 2 * (1 - 1 / (2**0.5))
pat = cairo.LinearGradient(3 + inset, 3 + inset, 21 - inset, 21 - inset)
pat.add_color_stop_rgb(0.0, *rgb(self._previous))
pat.add_color_stop_rgb(1.0, *rgb(self._active))
cr.set_source(pat)