color-sorter
Version:
Sort colors in a visually pleasing way.
46 lines (45 loc) • 1.28 kB
JavaScript
import { ColorSpace as n, sRGB as p, P3 as f, HSL as a, LCH as g, OKLCH as m, parse as C, to as L } from "colorjs.io/fn";
n.register(p);
n.register(f);
n.register(a);
n.register(g);
n.register(m);
function s(t) {
return typeof t == "number" && Number.isFinite(t) ? t : 0;
}
function i(t) {
try {
let r = C(t), o = r.spaceId === "hsl" ? r : L(r, a), e = o.coords, u = s(e[0]), l = s(e[1]), h = s(e[2]), c = s(o.alpha);
return {
hue: u,
saturation: l,
lightness: h,
alpha: c,
authored: t
};
} catch {
return {
hue: 0,
saturation: 0,
lightness: 0,
alpha: 0,
authored: t
};
}
}
function d(t, r) {
return (t.saturation === 0 || r.saturation === 0) && t.saturation !== r.saturation ? r.saturation - t.saturation : t.hue !== r.hue ? t.hue - r.hue : t.saturation !== r.saturation ? t.saturation - r.saturation : t.saturation === 0 && r.saturation === 0 && t.lightness !== r.lightness ? r.lightness - t.lightness : t.alpha === r.alpha ? t.authored.toLowerCase().localeCompare(r.authored.toLowerCase()) : r.alpha - t.alpha;
}
function y(t, r) {
let o = i(t), e = i(r);
return d(o, e);
}
function w(t) {
return t.sort(y);
}
export {
d as compare,
i as convert,
w as sort,
y as sortFn
};