Skip to content

Instantly share code, notes, and snippets.

@MarcoPolo
Created December 6, 2012 21:03
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save MarcoPolo/4228374 to your computer and use it in GitHub Desktop.
Convert hex color values to rgb clojure
(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])))
@Mariownyou
Copy link

How to convert HEX8 to rgb?

@kendagriff
Copy link

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