|
| 1 | +(ns day8.re-frame-10x.tools.identicon |
| 2 | + (:require [clojure.string :as str])) |
| 3 | + |
| 4 | +;; Ported from identicon.js by Don Park - https://github.com/donpark/identicon |
| 5 | +;; Stripped down for SVG only and converted to ClojureScript. |
| 6 | + |
| 7 | +(defn- build-grid [hash] |
| 8 | + ;; Original used 16 bytes from hash, we use the single 32-bit integer hash |
| 9 | + ;; Use bits from the hash to fill a 5x5 grid, mirroring the first 2 columns to the last 2 |
| 10 | + (let [;; Use lower bits for the grid pattern |
| 11 | + grid-pattern (bit-and hash 0xFFFFF) ;; Use 20 bits for 5x5 grid decisions |
| 12 | + grid (volatile! (vec (repeat 25 false)))] |
| 13 | + (dotimes [i 15] ;; Fill first 3 columns (15 cells) |
| 14 | + (when (pos? (bit-and grid-pattern (bit-shift-left 1 i))) |
| 15 | + (let [row (quot i 3) |
| 16 | + col (rem i 3)] |
| 17 | + (vswap! grid assoc (+ (* row 5) col) true)))) |
| 18 | + ;; Mirror columns 0 and 1 to 4 and 3 |
| 19 | + (dotimes [row 5] |
| 20 | + (vswap! grid assoc (+ (* row 5) 4) (get @grid (+ (* row 5) 0))) ;; col 4 = col 0 |
| 21 | + (vswap! grid assoc (+ (* row 5) 3) (get @grid (+ (* row 5) 1)))) ;; col 3 = col 1 |
| 22 | + @grid)) |
| 23 | + |
| 24 | +(defn- foreground-color [hash] |
| 25 | + ;; Use higher bits for color, ensure it's not too light. |
| 26 | + ;; Original used first 4 bytes of hash for HSL. We adapt for our single hash int. |
| 27 | + ;; Simple approach: use hash modulo 360 for hue, fixed saturation/lightness. |
| 28 | + (let [hue (mod (bit-shift-right hash 20) 360) ;; Use some higher bits for hue |
| 29 | + saturation 65 |
| 30 | + lightness 40] |
| 31 | + (str "hsl(" hue "," saturation "%," lightness "%)"))) |
| 32 | + |
| 33 | +(defn svg |
| 34 | + "Generates identicon SVG data as Hiccup. |
| 35 | + Takes a string value and an options map. |
| 36 | + Options: |
| 37 | + :size - width/height of the SVG (default 100) |
| 38 | + :background - vector [r g b a] 0-255 (default [240 240 240 255]) |
| 39 | + :margin - decimal percentage margin (default 0.08)" |
| 40 | + [value & {:keys [size background margin] |
| 41 | + :or {size 14, background "white", margin 0}}] |
| 42 | + (when-not (str/blank? value) |
| 43 | + (let [hash (hash-string value) |
| 44 | + fg-color (foreground-color hash) |
| 45 | + grid (build-grid hash) |
| 46 | + cell-size (/ size 5.0) |
| 47 | + base-margin (* size margin) |
| 48 | + cell-margin (/ base-margin 2.0) |
| 49 | + final-size (+ size base-margin)] |
| 50 | + (into [:svg {:width final-size |
| 51 | + :height final-size |
| 52 | + :viewBox (str "0 0 " final-size " " final-size) |
| 53 | + :xmlns "http://www.w3.org/2000/svg" |
| 54 | + :style {:background background}} |
| 55 | + [:text {:x 0 |
| 56 | + :y 0 |
| 57 | + :text-anchor "middle" |
| 58 | + :alignment-baseline "middle" |
| 59 | + :fill "rgba(0,0,0,0)" |
| 60 | + :style {:user-select :text}} |
| 61 | + (subs value 5)]] |
| 62 | + (for [row (range 5) |
| 63 | + col (range 5) |
| 64 | + :let [idx (+ (* row 5) col)] |
| 65 | + :when (get grid idx)] |
| 66 | + [:rect {:x (+ cell-margin (* col cell-size)) |
| 67 | + :y (+ cell-margin (* row cell-size)) |
| 68 | + :width cell-size |
| 69 | + :height cell-size |
| 70 | + :fill fg-color}]))))) |
| 71 | + |
| 72 | +;; Example usage (for testing in REPL): |
| 73 | +;; (render-svg "hello" {:size 50}) |
| 74 | +;; (render-svg "test" {:size 30 :margin 0.1 :background [255 0 0 128]}) |
0 commit comments