Last active
April 23, 2022 20:07
-
-
Save martinklepsch/8730542 to your computer and use it in GitHub Desktop.
Generate a random HEX color in cojure or clojurescript
This file contains hidden or 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
(require '[clojure.string :as s]) | |
;; My initial attempt | |
(def c [:0 :1 :2 :3 :4 :5 :6 :7 :8 :9 :A :B :C :D :E :F]) | |
(str "#" (s/join (map name (repeatedly 6 #(rand-nth c))))) | |
;; Another option not using keywords (/ht locks in #clojure) | |
(def c [0 1 2 3 4 5 6 7 8 9 \A \B \C \D \E \F]) | |
(str "#" (s/join (repeatedly 6 #(rand-nth c)))) | |
;; the last line can be simplified even more (/ht xificurC in #clojure) | |
(apply str \# (repeatedly 6 #(rand-nth c))) | |
;; This works on Clojure (/ht wei__ in #clojure) | |
(format "%x" (rand-int 16rFFFFFF)) | |
;; this works in Clojurescript | |
(.toString (rand-int 16rFFFFFF) 16) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(str/upper-case (format "%06x" (rand-int 16rFFFFFF)))
^ added the zero padding, thanks :)