Skip to content

Instantly share code, notes, and snippets.

@lozh
Created July 24, 2010 14:16
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 lozh/488714 to your computer and use it in GitHub Desktop.
Save lozh/488714 to your computer and use it in GitHub Desktop.
(defn rgb-distance-squared
"Euclidean distance squared between two rgb values"
[[r1 g1 b1] [r2 g2 b2]]
(let [r (- r1 r2)
g (- g1 g2)
b (- b1 b2)]
(+
(* r r)
(* g g)
(* b b))))
(def key-colours
{[224 41 224] :purple
[24 180 46] :green
[12 129 245] :blue
[254 232 23] :yellow
[233 233 233] :white
[245 27 55] :red
[231 119 41] :orange
})
(defn rgb-to-key-colour
"Find colour in colour map closest to the supplied [r g b] triple"
[rgb-triple colour-map]
(colour-map
(apply min-key (partial rgb-distance-squared rgb-triple) (keys colour-map))))
(rgb-to-key-colour [255 0 0] key-colours)
:red
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment