summaryrefslogtreecommitdiff
path: root/crates/typst-library
diff options
context:
space:
mode:
author+merlan #flirora <flirora@flirora.xyz>2025-06-27 05:26:15 -0400
committerGitHub <noreply@github.com>2025-06-27 09:26:15 +0000
commit584dd5fec69ad80feedf4018e02b427f2c812716 (patch)
treede385a34447fee22e90b163d9e521ff2953c48ef /crates/typst-library
parentb9f3a95e03bd792bca4926959eab5f9f6edd1a9d (diff)
Fix panic when sampling across two coincident gradient stops (#6166)
Diffstat (limited to 'crates/typst-library')
-rw-r--r--crates/typst-library/src/visualize/gradient.rs21
1 files changed, 7 insertions, 14 deletions
diff --git a/crates/typst-library/src/visualize/gradient.rs b/crates/typst-library/src/visualize/gradient.rs
index 5d7859a3..4917da68 100644
--- a/crates/typst-library/src/visualize/gradient.rs
+++ b/crates/typst-library/src/visualize/gradient.rs
@@ -1285,24 +1285,17 @@ fn process_stops(stops: &[Spanned<GradientStop>]) -> SourceResult<Vec<(Color, Ra
/// Sample the stops at a given position.
fn sample_stops(stops: &[(Color, Ratio)], mixing_space: ColorSpace, t: f64) -> Color {
let t = t.clamp(0.0, 1.0);
- let mut low = 0;
- let mut high = stops.len();
+ let mut 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 {
+ while stops.get(j + 1).is_some_and(|(_, r)| r.is_zero()) {
+ j += 1;
}
+ return stops[j].0;
}
- if low == 0 {
- 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];
let t = (t - pos_0.get()) / (pos_1.get() - pos_0.get());
Color::mix_iter(