Created
December 6, 2012 21:03
-
-
Save MarcoPolo/4228374 to your computer and use it in GitHub Desktop.
Convert hex color values to rgb clojure
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 css-color [color] | |
(let [ colors (rest (clojure.string/split color #"")) | |
red (take 2 colors) | |
green (take 2 (drop 2 colors)) | |
blue (take 2 (drop 4 colors))] | |
(map #(-> (conj % "0x") (clojure.string/join) (read-string)) [red green blue]))) |
Modified to support the absence of "#" and 3 character hex values:
(defn hex->rgb
"Converts a hex (3 or 6 digit) color to rgb."
[hex]
(let [colors (-> (string/replace hex #"#" "")
(string/split #""))
colors (if (= 3 (count colors))
(mapcat (partial repeat 2) colors)
colors)]
(map #(-> (conj % "0x") (string/join) (read-string))
[(take 2 colors)
(take 2 (drop 2 colors))
(take 2 (drop 4 colors))])))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to convert HEX8 to rgb?