From f39ae2f762f39c258440220bc6a0709e70fc8ee3 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 12 Aug 2022 06:01:21 -0700 Subject: [PATCH] background: avoid weird wraparound in radial gradients I was seeing a black "hole" in the center of this gradient: ``` background = { { source = { Gradient={ colors = {"rgb(45,26,109)", "black"}, orientation = { Radial={ cx = 0.75, cy = 0.75, radius = 1.25, } }, } }, width="100%", height="100%", }, ``` setting noise=0 "fixed" it, so this commit localizes that fix to the center of the gradient by preventing noise from wrapping around the gradient. --- wezterm-gui/src/termwindow/background.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/wezterm-gui/src/termwindow/background.rs b/wezterm-gui/src/termwindow/background.rs index a4c1803e7..629496b20 100644 --- a/wezterm-gui/src/termwindow/background.rs +++ b/wezterm-gui/src/termwindow/background.rs @@ -115,11 +115,24 @@ impl CachedGradient { let cy = fh * cy.unwrap_or(0.5); for (x, y, pixel) in imgbuf.enumerate_pixels_mut() { - let nx = noise(&rng, noise_amount); - let ny = noise(&rng, noise_amount); + let x = x as f64; + let y = y as f64; - let t = (nx + (x as f64 - cx).powi(2) + (ny + y as f64 - cy).powi(2)).sqrt() - / radius; + // If we are close to the center, stop applying noise, + // as the noise can wrap around and start using the + // color from the other end of the gradient and look weird + let nx = if ((cx - x).abs() as usize) < noise_amount { + 0. + } else { + noise(&rng, noise_amount) + }; + let ny = if ((cy - y).abs() as usize) < noise_amount { + 0. + } else { + noise(&rng, noise_amount) + }; + + let t = (nx + (x - cx).powi(2) + (ny + y - cy).powi(2)).sqrt() / radius; *pixel = to_pixel(grad.at(t)); } }