Skip to content

Instantly share code, notes, and snippets.

@escherize
Created August 21, 2014 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save escherize/69c3156d0308933a7ede to your computer and use it in GitHub Desktop.
Save escherize/69c3156d0308933a7ede to your computer and use it in GitHub Desktop.
;; Bryan Maass
;; blending adding subtracting colors in elisp
(defun red (rgb) (car rgb))
(defun green (rgb) (cadr rgb))
(defun blue (rgb) (caddr rgb))
(defun color->rgb (c)
(let ((step (if (= 7 (length c)) 2 1)))
(list (string-to-number (substring c 1 (+ 1 step)) 16)
(string-to-number (substring c (+ 1 step) (+ 1 (* 2 step))) 16)
(string-to-number (substring c (+ 1 (* 2 step)) (+ 1 (* 3 step))) 16))))
(defun rgb->color (rgb)
(concat "#"
(format "%02x" (red rgb))
(format "%02x" (green rgb))
(format "%02x" (blue rgb))))
(defun int->hex-str (n)
(cond
((> n 255) "FF")
((< n 0) "00")
(t (format "%02x" n))))
(defun color-fn (c1 c2 fn)
(let ((rgb1 (color->rgb c1))
(rgb2 (color->rgb c2)))
(rgb->color
(list (funcall fn (red rgb1) (red rgb2))
(funcall fn (green rgb1) (green rgb2))
(funcall fn (blue rgb1) (blue rgb2))))))
(defun color-add (c1 c2)
(color-fn c1 c2 (lambda (a b) (min 255 (+ a b)))))
(defun blend (c1 c2)
(color-fn c1 c2 (lambda (a b) (min 255 (max 0 (/ (+ a b) 2))))))
(defun color-minus (c1 c2)
(color-fn c1 c2 (lambda (a b) (max 0 (- a b)))))
(defun lighten (c) (color-add c "#111111"))
(defun darken (c) (color-minus c "#111111"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment