diff --git a/crates/typst-library/src/visualize/gradient.rs b/crates/typst-library/src/visualize/gradient.rs index e19b410bc..20539197c 100644 --- a/crates/typst-library/src/visualize/gradient.rs +++ b/crates/typst-library/src/visualize/gradient.rs @@ -1284,34 +1284,20 @@ fn process_stops(stops: &[Spanned]) -> SourceResult Color { + assert!(!stops.is_empty(), "Cannot sample from gradient with no stops"); + let t = t.clamp(0.0, 1.0); - let mut low = 0; - let mut high = stops.len(); + let j = stops.partition_point(|(_, ratio)| ratio.get() < t); - while low < high { - let mid = (low + high) / 2; - if stops[mid].1.get() < t { - low = mid + 1; - } else { - high = mid; - } + if j == 0 { + return stops[0].0; + } + if j == stops.len() { + return stops.last().unwrap().0; } - if low == 0 { - low = 1; - } - if t == 1.0 { - while stops[low - 1].1 >= stops[low].1 { - low -= 1; - } - } else { - while stops[low - 1].1 >= stops[low].1 { - low += 1; - } - } - - let (col_0, pos_0) = stops[low - 1]; - let (col_1, pos_1) = stops[low]; + let (col_0, pos_0) = stops[j - 1]; + let (col_1, pos_1) = stops[j]; debug_assert!(pos_0 < pos_1); debug_assert!(pos_0.get() <= t); debug_assert!(t <= pos_1.get()); diff --git a/tests/ref/issue-6162-coincident-gradient-stops-export-png.png b/tests/ref/issue-6162-coincident-gradient-stops-export-png.png index d269342c7..886ea195d 100644 Binary files a/tests/ref/issue-6162-coincident-gradient-stops-export-png.png and b/tests/ref/issue-6162-coincident-gradient-stops-export-png.png differ