🐛 Add better nil handling in interpolate-gradient when offset exceeds stops

When no gradient stop satisfies (<= offset (:offset %)),
d/index-of-pred returns nil. The previous code called (dec nil) in
the start binding before the nil check, throwing a
NullPointerException/ClassCastException. Guard the start binding with
a cond that handles nil before attempting dec.

Signed-off-by: Andrey Antukh <niwi@niwi.nz>
This commit is contained in:
Andrey Antukh 2026-04-14 12:30:30 +00:00
parent 7b0ea5968d
commit 08ca561667

View File

@ -720,8 +720,10 @@
(defn- offset-spread
[from to num]
(->> (range 0 num)
(map #(mth/precision (+ from (* (/ (- to from) (dec num)) %)) 2))))
(if (<= num 1)
[from]
(->> (range 0 num)
(map #(mth/precision (+ from (* (/ (- to from) (dec num)) %)) 2)))))
(defn uniform-spread?
"Checks if the gradient stops are spread uniformly"
@ -750,6 +752,9 @@
(defn interpolate-gradient
[stops offset]
(let [idx (d/index-of-pred stops #(<= offset (:offset %)))
start (if (= idx 0) (first stops) (get stops (dec idx)))
start (cond
(nil? idx) (last stops)
(= idx 0) (first stops)
:else (get stops (dec idx)))
end (if (nil? idx) (last stops) (get stops idx))]
(interpolate-color start end offset)))