Skip to content

Instantly share code, notes, and snippets.

@gfredericks
Created January 2, 2013 00:04
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 gfredericks/4431141 to your computer and use it in GitHub Desktop.
Save gfredericks/4431141 to your computer and use it in GitHub Desktop.
(def all-bit-length-triples
(sort-by (fn [a] (vec (cons (apply max a)
(concat
(sort a)
a))))
(for [a (range 9), b (range 9), c (range 9)]
[a b c])))
(defn bindex
"Given a number in (range 256), returns the position of the rightmost
set bit, where 0 returns 0 and 1 returns 8."
[n]
(if (= 255 n)
0
(loop [x 8, n n]
(if (or (zero? x) (odd? n))
x
(recur (dec x) (/ n 2))))))
(defn numbers-by-bindex
[n]
(filter (comp #{n} bindex) (range 256)))
(def all-colors
(for [[r g b] all-bit-length-triples,
r' (numbers-by-bindex r)
g' (numbers-by-bindex g)
b' (numbers-by-bindex b)]
[r' g' b']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment