0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-25 07:58:49 -05:00

Reimplement color helpers using google closure library facilities.

This commit is contained in:
Andrey Antukh 2016-04-02 22:43:05 +03:00
parent 86e7ffc04a
commit f21a377d84

View file

@ -7,18 +7,9 @@
(ns uxbox.util.color (ns uxbox.util.color
"Color conversion utils." "Color conversion utils."
(:require [cuerdas.core :as str])) (:require [cuerdas.core :as str]
[uxbox.util.math :as math]
(defn hex->rgb [goog.color :as gcolor]))
[^string data]
(some->> (re-find #"^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$" data)
(rest)
(mapv #(js/parseInt % 16))))
(defn hex->rgba
[^string data ^number opacity]
(-> (hex->rgb data)
(conj opacity)))
(defn rgb->str (defn rgb->str
[color] [color]
@ -27,11 +18,32 @@
(apply str/format "rgb(%s,%s,%s)" color) (apply str/format "rgb(%s,%s,%s)" color)
(apply str/format "rgba(%s,%s,%s,%s)" color))) (apply str/format "rgba(%s,%s,%s,%s)" color)))
(defn rgb->hsv
[[r g b]]
(into [] (gcolor/rgbToHsv r g b)))
(defn hsv->rgb
[[h s v]]
(into [] (gcolor/hsvToRgb h s v)))
(defn hex->rgb
[v]
(into [] (gcolor/hexToRgb v)))
(defn rgb->hex (defn rgb->hex
[[r g b]] [[r g b]]
(letfn [(to-hex [c] (gcolor/rgbToHex r g b))
(let [hexdata (.toString c 16)]
(if (= (count hexdata) 1) (defn hex->hsv
(str "0" hexdata) [v]
hexdata)))] (into [] (gcolor/hexToHsv v)))
(str "#" (to-hex r) (to-hex g) (to-hex b))))
(defn hsv->hex
[[h s v]]
(gcolor/hsvToHex h s v))
(defn hex->rgba
[^string data ^number opacity]
(-> (hex->rgb data)
(conj opacity)))