0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-13 16:21:57 -05:00

Merge pull request #4540 from penpot/azazeln28-fix-color-palette-sorting-by-hue-and-value

🐛  Color palette sorting
This commit is contained in:
Alejandro 2024-05-10 10:26:42 +02:00 committed by GitHub
commit c616e3c932
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 7 deletions

View file

@ -274,6 +274,13 @@
(catch #?(:clj Throwable :cljs :default) _cause
[0 0 0])))
(defn hex->lum
[color]
(let [[r g b] (hex->rgb color)]
(mth/sqrt (+ (* 0.241 r)
(* 0.691 g)
(* 0.068 b)))))
(defn- int->hex
"Convert integer to hex string"
[v]
@ -455,3 +462,19 @@
:else
[r g (inc b)]))
(defn reduce-range
[value range]
(/ (mth/floor (* value range)) range))
(defn sort-colors
[a b]
(let [[ah _ av] (hex->hsv (:color a))
[bh _ bv] (hex->hsv (:color b))
ah (reduce-range (/ ah 60) 8)
bh (reduce-range (/ bh 60) 8)
av (/ av 255)
bv (/ bv 255)
a (+ (* ah 100) (* av 10))
b (+ (* bh 100) (* bv 10))]
(compare a b)))

View file

@ -54,13 +54,7 @@
get-sorted-colors
(mf/use-fn
(fn [colors]
(sort (fn [a b]
(let [[ah _ al] (c/hex->hsl (:color a))
[bh _ bl] (c/hex->hsl (:color b))
a (+ (* ah 100) (* al 99))
b (+ (* bh 100) (* bl 99))]
(compare a b)))
(into [] (filter check-valid-color?) colors))))
(sort c/sort-colors (into [] (filter check-valid-color?) colors))))
toggle-palette
(mf/use-fn