Skip to content

Instantly share code, notes, and snippets.

@dsp
Created February 3, 2010 23:09
Show Gist options
  • Save dsp/294152 to your computer and use it in GitHub Desktop.
Save dsp/294152 to your computer and use it in GitHub Desktop.
HSV Colorspace to RGB Colorspace transformation and vice versa
(defn hsv2rgb
([x] (apply hsv2rgb x))
([h s v] (map #(* 255 %)
(let [u (int (/ h 60.0))
f (- (/ h 60.0) u)
p (* v (- 1 s))
q (* v (- 1 (* s f)))
t (* v (- 1 (* s (- 1 f))))]
(condp = u
0 (list v t q)
1 (list q v p)
2 (list p v t)
3 (list p q v)
4 (list t p v)
5 (list v p q))))))
(defn rgb2hsv
([x] (apply rgb2hsv x))
([r g b] (map #(float %)
(let [m (max r g b)
n (min r g b)
div (- m n)]
(list (cond
(<= div 0) 0
(= m r) (mod (+ (* 60 (/ (- g b) div)) 360) 360)
(= m g) (+ (* 60 (/ (- b r) div)) 120)
(= m b) (+ (* 60 (/ (- r g) div)) 240))
(if (= m 0) 0 (/ div m))
(/ m 255))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment