mirror of
https://github.com/typst/typst
synced 2025-08-27 13:14:12 +08:00
Fix panic when sampling across two coincident gradient stops (#6166)
This commit is contained in:
parent
b9f3a95e03
commit
584dd5fec6
@ -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(
|
||||
|
BIN
tests/ref/issue-6162-coincident-gradient-stops-export-png.png
Normal file
BIN
tests/ref/issue-6162-coincident-gradient-stops-export-png.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 522 B |
@ -666,3 +666,29 @@ $ A = mat(
|
||||
#let _ = gradient.linear(..my-gradient.stops())
|
||||
#let my-gradient2 = gradient.linear(red, blue).repeat(5, mirror: true)
|
||||
#let _ = gradient.linear(..my-gradient2.stops())
|
||||
|
||||
--- issue-6162-coincident-gradient-stops-export-png ---
|
||||
// Ensure that multiple gradient stops with the same position
|
||||
// don't cause a panic.
|
||||
#rect(
|
||||
fill: gradient.linear(
|
||||
(red, 0%),
|
||||
(green, 0%),
|
||||
(blue, 100%),
|
||||
)
|
||||
)
|
||||
#rect(
|
||||
fill: gradient.linear(
|
||||
(red, 0%),
|
||||
(green, 100%),
|
||||
(blue, 100%),
|
||||
)
|
||||
)
|
||||
#rect(
|
||||
fill: gradient.linear(
|
||||
(white, 0%),
|
||||
(red, 50%),
|
||||
(green, 50%),
|
||||
(blue, 100%),
|
||||
)
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user