mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-11 21:02:45 +00:00
Clamp midpoint in Gradient::expanded_stops like sample does
Found while testing crates/color/src/gradient.rs:~101-110 (expanded_stops); the unclamped value comes from crates/idml/src/import/swatches.rs:128-129 Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
19c9875770
commit
c73699374f
1 file changed
+14
-2
@@ -100,10 +100,11 @@ impl Gradient {
|
||||
let mut out = Vec::new();
|
||||
for (i, s) in self.stops.iter().enumerate() {
|
||||
out.push((s.offset, s.color, s.opacity));
|
||||
let mid = s.midpoint.clamp(0.01, 0.99);
|
||||
if let Some(n) = self.stops.get(i + 1)
|
||||
&& (s.midpoint - 0.5).abs() > 1e-3
|
||||
&& (mid - 0.5).abs() > 1e-3
|
||||
{
|
||||
let t = s.offset + (n.offset - s.offset) * s.midpoint;
|
||||
let t = s.offset + (n.offset - s.offset) * mid;
|
||||
let (c, o) = self.sample(t);
|
||||
out.push((t, c, o));
|
||||
}
|
||||
@@ -217,6 +218,17 @@ impl GradientPaint {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn expanded_stops_clamp_out_of_range_midpoint() {
|
||||
for mid in [2.0, -1.0] {
|
||||
let mut g = Gradient::default();
|
||||
g.stops[0].midpoint = mid;
|
||||
let offsets: Vec<f32> = g.expanded_stops().iter().map(|s| s.0).collect();
|
||||
assert!(offsets.windows(2).all(|w| w[0] <= w[1]), "{offsets:?}");
|
||||
assert!(offsets.iter().all(|o| (0.0..=1.0).contains(o)), "{offsets:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_endpoints_and_mid() {
|
||||
let g = Gradient::default();
|
||||
|
||||
Reference in new issue
Block a user