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.
This commit is contained in:
Wez Furlong committed 2022-08-14 08:25:31 -07:00
1 parent f6f36b454b
commit f39ae2f762
1 file changed
+17 -4
+17 -4
View File
@@ -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));
}
}