Skip to content

Commit

Permalink
Add lerp and gradient evaluation functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinmera committed Jan 17, 2020
1 parent f23d26f commit 23bc4b0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 16 additions & 0 deletions ops.lisp
Expand Up @@ -235,3 +235,19 @@
(expt (- 1 (exp (* exposure (- (g color))))) (/ gamma))
(expt (- 1 (exp (* exposure (- (b color))))) (/ gamma))
(a color)))

(defun lerp (x a b)
(macrolet ((lerp (f)
`(+ (* (,f a) (- 1 x)) (* (,f b) x))))
(color (lerp r) (lerp g) (lerp b) (lerp a))))

(defun gradient (x stops)
(if (<= x 0)
(cdr (first stops))
(loop for prev = (first stops) then next
for next in (rest stops)
do (destructuring-bind (px . pc) prev
(destructuring-bind (nx . nc) next
(when (<= px x nx)
(return (lerp (/ (- x px) (- nx px)) pc nc)))))
finally (return (cdr prev)))))
4 changes: 3 additions & 1 deletion package.lisp
Expand Up @@ -54,7 +54,9 @@
#:map-color
#:gamma-adjust
#:reinhard-map
#:exposure-map)
#:exposure-map
#:lerp
#:gradient)
;; constants.lisp
(:export
#:define-color))
Expand Down

0 comments on commit 23bc4b0

Please sign in to comment.