Created
July 24, 2010 14:16
-
-
Save lozh/488714 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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