colour-swapper
Version:
colour-swapper is a compact and efficient JavaScript library designed for color manipulation and conversion
815 lines (814 loc) • 23.3 kB
JavaScript
function _(e) {
"@babel/helpers - typeof";
return _ = typeof Symbol == "function" && typeof Symbol.iterator == "symbol" ? function(r) {
return typeof r;
} : function(r) {
return r && typeof Symbol == "function" && r.constructor === Symbol && r !== Symbol.prototype ? "symbol" : typeof r;
}, _(e);
}
var T = /^\s+/, N = /\s+$/;
function s(e, r) {
if (e = e || "", r = r || {}, e instanceof s)
return e;
if (!(this instanceof s))
return new s(e, r);
var t = E(e);
this._originalInput = e, this._r = t.r, this._g = t.g, this._b = t.b, this._a = t.a, this._roundA = Math.round(100 * this._a) / 100, this._format = r.format || t.format, this._gradientType = r.gradientType, this._r < 1 && (this._r = Math.round(this._r)), this._g < 1 && (this._g = Math.round(this._g)), this._b < 1 && (this._b = Math.round(this._b)), this._ok = t.ok;
}
s.prototype = {
isDark: function() {
return this.getBrightness() < 128;
},
isLight: function() {
return !this.isDark();
},
isValid: function() {
return this._ok;
},
getOriginalInput: function() {
return this._originalInput;
},
getFormat: function() {
return this._format;
},
getAlpha: function() {
return this._a;
},
getBrightness: function() {
var r = this.toRgb();
return (r.r * 299 + r.g * 587 + r.b * 114) / 1e3;
},
getLuminance: function() {
var r = this.toRgb(), t, n, a, i, h, f;
return t = r.r / 255, n = r.g / 255, a = r.b / 255, t <= 0.03928 ? i = t / 12.92 : i = Math.pow((t + 0.055) / 1.055, 2.4), n <= 0.03928 ? h = n / 12.92 : h = Math.pow((n + 0.055) / 1.055, 2.4), a <= 0.03928 ? f = a / 12.92 : f = Math.pow((a + 0.055) / 1.055, 2.4), 0.2126 * i + 0.7152 * h + 0.0722 * f;
},
setAlpha: function(r) {
return this._a = I(r), this._roundA = Math.round(100 * this._a) / 100, this;
},
toHsv: function() {
var r = S(this._r, this._g, this._b);
return {
h: r.h * 360,
s: r.s,
v: r.v,
a: this._a
};
},
toHsvString: function() {
var r = S(this._r, this._g, this._b), t = Math.round(r.h * 360), n = Math.round(r.s * 100), a = Math.round(r.v * 100);
return this._a == 1 ? "hsv(" + t + ", " + n + "%, " + a + "%)" : "hsva(" + t + ", " + n + "%, " + a + "%, " + this._roundA + ")";
},
toHsl: function() {
var r = A(this._r, this._g, this._b);
return {
h: r.h * 360,
s: r.s,
l: r.l,
a: this._a
};
},
toHslString: function() {
var r = A(this._r, this._g, this._b), t = Math.round(r.h * 360), n = Math.round(r.s * 100), a = Math.round(r.l * 100);
return this._a == 1 ? "hsl(" + t + ", " + n + "%, " + a + "%)" : "hsla(" + t + ", " + n + "%, " + a + "%, " + this._roundA + ")";
},
toHex: function(r) {
return k(this._r, this._g, this._b, r);
},
toHexString: function(r) {
return "#" + this.toHex(r);
},
toHex8: function(r) {
return B(this._r, this._g, this._b, this._a, r);
},
toHex8String: function(r) {
return "#" + this.toHex8(r);
},
toRgb: function() {
return {
r: Math.round(this._r),
g: Math.round(this._g),
b: Math.round(this._b),
a: this._a
};
},
toRgbString: function() {
return this._a == 1 ? "rgb(" + Math.round(this._r) + ", " + Math.round(this._g) + ", " + Math.round(this._b) + ")" : "rgba(" + Math.round(this._r) + ", " + Math.round(this._g) + ", " + Math.round(this._b) + ", " + this._roundA + ")";
},
toPercentageRgb: function() {
return {
r: Math.round(l(this._r, 255) * 100) + "%",
g: Math.round(l(this._g, 255) * 100) + "%",
b: Math.round(l(this._b, 255) * 100) + "%",
a: this._a
};
},
toPercentageRgbString: function() {
return this._a == 1 ? "rgb(" + Math.round(l(this._r, 255) * 100) + "%, " + Math.round(l(this._g, 255) * 100) + "%, " + Math.round(l(this._b, 255) * 100) + "%)" : "rgba(" + Math.round(l(this._r, 255) * 100) + "%, " + Math.round(l(this._g, 255) * 100) + "%, " + Math.round(l(this._b, 255) * 100) + "%, " + this._roundA + ")";
},
toName: function() {
return this._a === 0 ? "transparent" : this._a < 1 ? !1 : Z[k(this._r, this._g, this._b, !0)] || !1;
},
toFilter: function(r) {
var t = "#" + H(this._r, this._g, this._b, this._a), n = t, a = this._gradientType ? "GradientType = 1, " : "";
if (r) {
var i = s(r);
n = "#" + H(i._r, i._g, i._b, i._a);
}
return "progid:DXImageTransform.Microsoft.gradient(" + a + "startColorstr=" + t + ",endColorstr=" + n + ")";
},
toString: function(r) {
var t = !!r;
r = r || this._format;
var n = !1, a = this._a < 1 && this._a >= 0, i = !t && a && (r === "hex" || r === "hex6" || r === "hex3" || r === "hex4" || r === "hex8" || r === "name");
return i ? r === "name" && this._a === 0 ? this.toName() : this.toRgbString() : (r === "rgb" && (n = this.toRgbString()), r === "prgb" && (n = this.toPercentageRgbString()), (r === "hex" || r === "hex6") && (n = this.toHexString()), r === "hex3" && (n = this.toHexString(!0)), r === "hex4" && (n = this.toHex8String(!0)), r === "hex8" && (n = this.toHex8String()), r === "name" && (n = this.toName()), r === "hsl" && (n = this.toHslString()), r === "hsv" && (n = this.toHsvString()), n || this.toHexString());
},
clone: function() {
return s(this.toString());
},
_applyModification: function(r, t) {
var n = r.apply(null, [this].concat([].slice.call(t)));
return this._r = n._r, this._g = n._g, this._b = n._b, this.setAlpha(n._a), this;
},
lighten: function() {
return this._applyModification(j, arguments);
},
brighten: function() {
return this._applyModification(q, arguments);
},
darken: function() {
return this._applyModification(U, arguments);
},
desaturate: function() {
return this._applyModification(G, arguments);
},
saturate: function() {
return this._applyModification(L, arguments);
},
greyscale: function() {
return this._applyModification(D, arguments);
},
spin: function() {
return this._applyModification(V, arguments);
},
_applyCombination: function(r, t) {
return r.apply(null, [this].concat([].slice.call(t)));
},
analogous: function() {
return this._applyCombination(W, arguments);
},
complement: function() {
return this._applyCombination($, arguments);
},
monochromatic: function() {
return this._applyCombination(Y, arguments);
},
splitcomplement: function() {
return this._applyCombination(X, arguments);
},
// Disabled until https://github.com/bgrins/TinyColor/issues/254
// polyad: function (number) {
// return this._applyCombination(polyad, [number]);
// },
triad: function() {
return this._applyCombination(R, [3]);
},
tetrad: function() {
return this._applyCombination(R, [4]);
}
};
s.fromRatio = function(e, r) {
if (_(e) == "object") {
var t = {};
for (var n in e)
e.hasOwnProperty(n) && (n === "a" ? t[n] = e[n] : t[n] = v(e[n]));
e = t;
}
return s(e, r);
};
function E(e) {
var r = {
r: 0,
g: 0,
b: 0
}, t = 1, n = null, a = null, i = null, h = !1, f = !1;
return typeof e == "string" && (e = ee(e)), _(e) == "object" && (m(e.r) && m(e.g) && m(e.b) ? (r = P(e.r, e.g, e.b), h = !0, f = String(e.r).substr(-1) === "%" ? "prgb" : "rgb") : m(e.h) && m(e.s) && m(e.v) ? (n = v(e.s), a = v(e.v), r = O(e.h, n, a), h = !0, f = "hsv") : m(e.h) && m(e.s) && m(e.l) && (n = v(e.s), i = v(e.l), r = z(e.h, n, i), h = !0, f = "hsl"), e.hasOwnProperty("a") && (t = e.a)), t = I(t), {
ok: h,
format: e.format || f,
r: Math.min(255, Math.max(r.r, 0)),
g: Math.min(255, Math.max(r.g, 0)),
b: Math.min(255, Math.max(r.b, 0)),
a: t
};
}
function P(e, r, t) {
return {
r: l(e, 255) * 255,
g: l(r, 255) * 255,
b: l(t, 255) * 255
};
}
function A(e, r, t) {
e = l(e, 255), r = l(r, 255), t = l(t, 255);
var n = Math.max(e, r, t), a = Math.min(e, r, t), i, h, f = (n + a) / 2;
if (n == a)
i = h = 0;
else {
var o = n - a;
switch (h = f > 0.5 ? o / (2 - n - a) : o / (n + a), n) {
case e:
i = (r - t) / o + (r < t ? 6 : 0);
break;
case r:
i = (t - e) / o + 2;
break;
case t:
i = (e - r) / o + 4;
break;
}
i /= 6;
}
return {
h: i,
s: h,
l: f
};
}
function z(e, r, t) {
var n, a, i;
e = l(e, 360), r = l(r, 100), t = l(t, 100);
function h(u, p, d) {
return d < 0 && (d += 1), d > 1 && (d -= 1), d < 1 / 6 ? u + (p - u) * 6 * d : d < 1 / 2 ? p : d < 2 / 3 ? u + (p - u) * (2 / 3 - d) * 6 : u;
}
if (r === 0)
n = a = i = t;
else {
var f = t < 0.5 ? t * (1 + r) : t + r - t * r, o = 2 * t - f;
n = h(o, f, e + 1 / 3), a = h(o, f, e), i = h(o, f, e - 1 / 3);
}
return {
r: n * 255,
g: a * 255,
b: i * 255
};
}
function S(e, r, t) {
e = l(e, 255), r = l(r, 255), t = l(t, 255);
var n = Math.max(e, r, t), a = Math.min(e, r, t), i, h, f = n, o = n - a;
if (h = n === 0 ? 0 : o / n, n == a)
i = 0;
else {
switch (n) {
case e:
i = (r - t) / o + (r < t ? 6 : 0);
break;
case r:
i = (t - e) / o + 2;
break;
case t:
i = (e - r) / o + 4;
break;
}
i /= 6;
}
return {
h: i,
s: h,
v: f
};
}
function O(e, r, t) {
e = l(e, 360) * 6, r = l(r, 100), t = l(t, 100);
var n = Math.floor(e), a = e - n, i = t * (1 - r), h = t * (1 - a * r), f = t * (1 - (1 - a) * r), o = n % 6, u = [t, h, i, i, f, t][o], p = [f, t, t, h, i, i][o], d = [i, i, f, t, t, h][o];
return {
r: u * 255,
g: p * 255,
b: d * 255
};
}
function k(e, r, t, n) {
var a = [b(Math.round(e).toString(16)), b(Math.round(r).toString(16)), b(Math.round(t).toString(16))];
return n && a[0].charAt(0) == a[0].charAt(1) && a[1].charAt(0) == a[1].charAt(1) && a[2].charAt(0) == a[2].charAt(1) ? a[0].charAt(0) + a[1].charAt(0) + a[2].charAt(0) : a.join("");
}
function B(e, r, t, n, a) {
var i = [b(Math.round(e).toString(16)), b(Math.round(r).toString(16)), b(Math.round(t).toString(16)), b(F(n))];
return a && i[0].charAt(0) == i[0].charAt(1) && i[1].charAt(0) == i[1].charAt(1) && i[2].charAt(0) == i[2].charAt(1) && i[3].charAt(0) == i[3].charAt(1) ? i[0].charAt(0) + i[1].charAt(0) + i[2].charAt(0) + i[3].charAt(0) : i.join("");
}
function H(e, r, t, n) {
var a = [b(F(n)), b(Math.round(e).toString(16)), b(Math.round(r).toString(16)), b(Math.round(t).toString(16))];
return a.join("");
}
s.equals = function(e, r) {
return !e || !r ? !1 : s(e).toRgbString() == s(r).toRgbString();
};
s.random = function() {
return s.fromRatio({
r: Math.random(),
g: Math.random(),
b: Math.random()
});
};
function G(e, r) {
r = r === 0 ? 0 : r || 10;
var t = s(e).toHsl();
return t.s -= r / 100, t.s = x(t.s), s(t);
}
function L(e, r) {
r = r === 0 ? 0 : r || 10;
var t = s(e).toHsl();
return t.s += r / 100, t.s = x(t.s), s(t);
}
function D(e) {
return s(e).desaturate(100);
}
function j(e, r) {
r = r === 0 ? 0 : r || 10;
var t = s(e).toHsl();
return t.l += r / 100, t.l = x(t.l), s(t);
}
function q(e, r) {
r = r === 0 ? 0 : r || 10;
var t = s(e).toRgb();
return t.r = Math.max(0, Math.min(255, t.r - Math.round(255 * -(r / 100)))), t.g = Math.max(0, Math.min(255, t.g - Math.round(255 * -(r / 100)))), t.b = Math.max(0, Math.min(255, t.b - Math.round(255 * -(r / 100)))), s(t);
}
function U(e, r) {
r = r === 0 ? 0 : r || 10;
var t = s(e).toHsl();
return t.l -= r / 100, t.l = x(t.l), s(t);
}
function V(e, r) {
var t = s(e).toHsl(), n = (t.h + r) % 360;
return t.h = n < 0 ? 360 + n : n, s(t);
}
function $(e) {
var r = s(e).toHsl();
return r.h = (r.h + 180) % 360, s(r);
}
function R(e, r) {
if (isNaN(r) || r <= 0)
throw new Error("Argument to polyad must be a positive number");
for (var t = s(e).toHsl(), n = [s(e)], a = 360 / r, i = 1; i < r; i++)
n.push(s({
h: (t.h + i * a) % 360,
s: t.s,
l: t.l
}));
return n;
}
function X(e) {
var r = s(e).toHsl(), t = r.h;
return [s(e), s({
h: (t + 72) % 360,
s: r.s,
l: r.l
}), s({
h: (t + 216) % 360,
s: r.s,
l: r.l
})];
}
function W(e, r, t) {
r = r || 6, t = t || 30;
var n = s(e).toHsl(), a = 360 / t, i = [s(e)];
for (n.h = (n.h - (a * r >> 1) + 720) % 360; --r; )
n.h = (n.h + a) % 360, i.push(s(n));
return i;
}
function Y(e, r) {
r = r || 6;
for (var t = s(e).toHsv(), n = t.h, a = t.s, i = t.v, h = [], f = 1 / r; r--; )
h.push(s({
h: n,
s: a,
v: i
})), i = (i + f) % 1;
return h;
}
s.mix = function(e, r, t) {
t = t === 0 ? 0 : t || 50;
var n = s(e).toRgb(), a = s(r).toRgb(), i = t / 100, h = {
r: (a.r - n.r) * i + n.r,
g: (a.g - n.g) * i + n.g,
b: (a.b - n.b) * i + n.b,
a: (a.a - n.a) * i + n.a
};
return s(h);
};
s.readability = function(e, r) {
var t = s(e), n = s(r);
return (Math.max(t.getLuminance(), n.getLuminance()) + 0.05) / (Math.min(t.getLuminance(), n.getLuminance()) + 0.05);
};
s.isReadable = function(e, r, t) {
var n = s.readability(e, r), a, i;
switch (i = !1, a = te(t), a.level + a.size) {
case "AAsmall":
case "AAAlarge":
i = n >= 4.5;
break;
case "AAlarge":
i = n >= 3;
break;
case "AAAsmall":
i = n >= 7;
break;
}
return i;
};
s.mostReadable = function(e, r, t) {
var n = null, a = 0, i, h, f, o;
t = t || {}, h = t.includeFallbackColors, f = t.level, o = t.size;
for (var u = 0; u < r.length; u++)
i = s.readability(e, r[u]), i > a && (a = i, n = s(r[u]));
return s.isReadable(e, n, {
level: f,
size: o
}) || !h ? n : (t.includeFallbackColors = !1, s.mostReadable(e, ["#fff", "#000"], t));
};
var y = s.names = {
aliceblue: "f0f8ff",
antiquewhite: "faebd7",
aqua: "0ff",
aquamarine: "7fffd4",
azure: "f0ffff",
beige: "f5f5dc",
bisque: "ffe4c4",
black: "000",
blanchedalmond: "ffebcd",
blue: "00f",
blueviolet: "8a2be2",
brown: "a52a2a",
burlywood: "deb887",
burntsienna: "ea7e5d",
cadetblue: "5f9ea0",
chartreuse: "7fff00",
chocolate: "d2691e",
coral: "ff7f50",
cornflowerblue: "6495ed",
cornsilk: "fff8dc",
crimson: "dc143c",
cyan: "0ff",
darkblue: "00008b",
darkcyan: "008b8b",
darkgoldenrod: "b8860b",
darkgray: "a9a9a9",
darkgreen: "006400",
darkgrey: "a9a9a9",
darkkhaki: "bdb76b",
darkmagenta: "8b008b",
darkolivegreen: "556b2f",
darkorange: "ff8c00",
darkorchid: "9932cc",
darkred: "8b0000",
darksalmon: "e9967a",
darkseagreen: "8fbc8f",
darkslateblue: "483d8b",
darkslategray: "2f4f4f",
darkslategrey: "2f4f4f",
darkturquoise: "00ced1",
darkviolet: "9400d3",
deeppink: "ff1493",
deepskyblue: "00bfff",
dimgray: "696969",
dimgrey: "696969",
dodgerblue: "1e90ff",
firebrick: "b22222",
floralwhite: "fffaf0",
forestgreen: "228b22",
fuchsia: "f0f",
gainsboro: "dcdcdc",
ghostwhite: "f8f8ff",
gold: "ffd700",
goldenrod: "daa520",
gray: "808080",
green: "008000",
greenyellow: "adff2f",
grey: "808080",
honeydew: "f0fff0",
hotpink: "ff69b4",
indianred: "cd5c5c",
indigo: "4b0082",
ivory: "fffff0",
khaki: "f0e68c",
lavender: "e6e6fa",
lavenderblush: "fff0f5",
lawngreen: "7cfc00",
lemonchiffon: "fffacd",
lightblue: "add8e6",
lightcoral: "f08080",
lightcyan: "e0ffff",
lightgoldenrodyellow: "fafad2",
lightgray: "d3d3d3",
lightgreen: "90ee90",
lightgrey: "d3d3d3",
lightpink: "ffb6c1",
lightsalmon: "ffa07a",
lightseagreen: "20b2aa",
lightskyblue: "87cefa",
lightslategray: "789",
lightslategrey: "789",
lightsteelblue: "b0c4de",
lightyellow: "ffffe0",
lime: "0f0",
limegreen: "32cd32",
linen: "faf0e6",
magenta: "f0f",
maroon: "800000",
mediumaquamarine: "66cdaa",
mediumblue: "0000cd",
mediumorchid: "ba55d3",
mediumpurple: "9370db",
mediumseagreen: "3cb371",
mediumslateblue: "7b68ee",
mediumspringgreen: "00fa9a",
mediumturquoise: "48d1cc",
mediumvioletred: "c71585",
midnightblue: "191970",
mintcream: "f5fffa",
mistyrose: "ffe4e1",
moccasin: "ffe4b5",
navajowhite: "ffdead",
navy: "000080",
oldlace: "fdf5e6",
olive: "808000",
olivedrab: "6b8e23",
orange: "ffa500",
orangered: "ff4500",
orchid: "da70d6",
palegoldenrod: "eee8aa",
palegreen: "98fb98",
paleturquoise: "afeeee",
palevioletred: "db7093",
papayawhip: "ffefd5",
peachpuff: "ffdab9",
peru: "cd853f",
pink: "ffc0cb",
plum: "dda0dd",
powderblue: "b0e0e6",
purple: "800080",
rebeccapurple: "663399",
red: "f00",
rosybrown: "bc8f8f",
royalblue: "4169e1",
saddlebrown: "8b4513",
salmon: "fa8072",
sandybrown: "f4a460",
seagreen: "2e8b57",
seashell: "fff5ee",
sienna: "a0522d",
silver: "c0c0c0",
skyblue: "87ceeb",
slateblue: "6a5acd",
slategray: "708090",
slategrey: "708090",
snow: "fffafa",
springgreen: "00ff7f",
steelblue: "4682b4",
tan: "d2b48c",
teal: "008080",
thistle: "d8bfd8",
tomato: "ff6347",
turquoise: "40e0d0",
violet: "ee82ee",
wheat: "f5deb3",
white: "fff",
whitesmoke: "f5f5f5",
yellow: "ff0",
yellowgreen: "9acd32"
}, Z = s.hexNames = J(y);
function J(e) {
var r = {};
for (var t in e)
e.hasOwnProperty(t) && (r[e[t]] = t);
return r;
}
function I(e) {
return e = parseFloat(e), (isNaN(e) || e < 0 || e > 1) && (e = 1), e;
}
function l(e, r) {
K(e) && (e = "100%");
var t = Q(e);
return e = Math.min(r, Math.max(0, parseFloat(e))), t && (e = parseInt(e * r, 10) / 100), Math.abs(e - r) < 1e-6 ? 1 : e % r / parseFloat(r);
}
function x(e) {
return Math.min(1, Math.max(0, e));
}
function g(e) {
return parseInt(e, 16);
}
function K(e) {
return typeof e == "string" && e.indexOf(".") != -1 && parseFloat(e) === 1;
}
function Q(e) {
return typeof e == "string" && e.indexOf("%") != -1;
}
function b(e) {
return e.length == 1 ? "0" + e : "" + e;
}
function v(e) {
return e <= 1 && (e = e * 100 + "%"), e;
}
function F(e) {
return Math.round(parseFloat(e) * 255).toString(16);
}
function w(e) {
return g(e) / 255;
}
var c = function() {
var e = "[-\\+]?\\d+%?", r = "[-\\+]?\\d*\\.\\d+%?", t = "(?:" + r + ")|(?:" + e + ")", n = "[\\s|\\(]+(" + t + ")[,|\\s]+(" + t + ")[,|\\s]+(" + t + ")\\s*\\)?", a = "[\\s|\\(]+(" + t + ")[,|\\s]+(" + t + ")[,|\\s]+(" + t + ")[,|\\s]+(" + t + ")\\s*\\)?";
return {
CSS_UNIT: new RegExp(t),
rgb: new RegExp("rgb" + n),
rgba: new RegExp("rgba" + a),
hsl: new RegExp("hsl" + n),
hsla: new RegExp("hsla" + a),
hsv: new RegExp("hsv" + n),
hsva: new RegExp("hsva" + a),
hex3: /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,
hex6: /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/,
hex4: /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,
hex8: /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/
};
}();
function m(e) {
return !!c.CSS_UNIT.exec(e);
}
function ee(e) {
e = e.replace(T, "").replace(N, "").toLowerCase();
var r = !1;
if (y[e])
e = y[e], r = !0;
else if (e == "transparent")
return {
r: 0,
g: 0,
b: 0,
a: 0,
format: "name"
};
var t;
return (t = c.rgb.exec(e)) ? {
r: t[1],
g: t[2],
b: t[3]
} : (t = c.rgba.exec(e)) ? {
r: t[1],
g: t[2],
b: t[3],
a: t[4]
} : (t = c.hsl.exec(e)) ? {
h: t[1],
s: t[2],
l: t[3]
} : (t = c.hsla.exec(e)) ? {
h: t[1],
s: t[2],
l: t[3],
a: t[4]
} : (t = c.hsv.exec(e)) ? {
h: t[1],
s: t[2],
v: t[3]
} : (t = c.hsva.exec(e)) ? {
h: t[1],
s: t[2],
v: t[3],
a: t[4]
} : (t = c.hex8.exec(e)) ? {
r: g(t[1]),
g: g(t[2]),
b: g(t[3]),
a: w(t[4]),
format: r ? "name" : "hex8"
} : (t = c.hex6.exec(e)) ? {
r: g(t[1]),
g: g(t[2]),
b: g(t[3]),
format: r ? "name" : "hex"
} : (t = c.hex4.exec(e)) ? {
r: g(t[1] + "" + t[1]),
g: g(t[2] + "" + t[2]),
b: g(t[3] + "" + t[3]),
a: w(t[4] + "" + t[4]),
format: r ? "name" : "hex8"
} : (t = c.hex3.exec(e)) ? {
r: g(t[1] + "" + t[1]),
g: g(t[2] + "" + t[2]),
b: g(t[3] + "" + t[3]),
format: r ? "name" : "hex"
} : !1;
}
function te(e) {
var r, t;
return e = e || {
level: "AA",
size: "small"
}, r = (e.level || "AA").toUpperCase(), t = (e.size || "small").toLowerCase(), r !== "AA" && r !== "AAA" && (r = "AA"), t !== "small" && t !== "large" && (t = "small"), {
level: r,
size: t
};
}
const ae = (e) => {
const r = s(e).toHsl().h, t = s(e).toHsl().s, n = s(e).toHsl();
n.h = Number(r), t !== null && (n.s = Number(t) / 100);
const a = s(n).toHexString();
return s(a).greyscale().toHexString();
}, re = ({
paletteColors: e,
paletteColor: r,
fillColor: t
}) => {
const n = {
...t
};
if (r.l === 1) {
const a = e.filter(
(i) => !(i.l === 0 || i.l === 1 || i.s === 0)
);
if (a.length) {
const i = a[M(a, r).index];
n.h = i.h;
} else
n.s = 0;
} else
r.s === 0 || r.l === 0 ? n.s = 0 : n.h = r.h;
return n;
}, M = (e, r) => {
const t = e.map(
(a) => ne({ colorX: r, colorY: a })
), n = Math.min.apply(Math, t);
return {
distance: n,
index: t.indexOf(n)
};
}, ne = ({
colorX: e,
colorY: r
}) => {
const t = C(e), n = C(r);
return Math.hypot(n.x - t.x, n.y - t.y, n.z - t.z);
}, C = (e) => {
const r = s(e).toRgb();
return {
x: r.r,
y: r.g,
z: r.b
};
}, ie = (e, r) => {
const t = e.map((a, i) => ({
hsl: s(a).toHsl(),
index: i,
// Original index
isMostNearer: !1,
hex: void 0
})), n = r.map(
(a, i) => ({
hsl: s(a).toHsl(),
index: i,
fillColorIndexes: [],
// Array of nearest fill color's indexes
hex: void 0
})
);
return t.forEach((a, i) => {
const h = M(
n.map((o) => o.hsl),
a.hsl
);
n[h.index].fillColorIndexes.push({
index: i,
distance: h.distance
});
}), n.forEach((a) => {
a.fillColorIndexes = a.fillColorIndexes.sort(
(i, h) => i.distance - h.distance
), a.fillColorIndexes.length && (t[a.fillColorIndexes[0].index].isMostNearer = !0);
}), n.filter((a) => !a.fillColorIndexes.length).forEach((a) => {
const i = t.filter((h) => !h.isMostNearer);
if (i.length) {
const h = M(
i.map((u) => u.hex),
a.hex
), f = i[h.index], o = t[f.index];
n.filter((u) => u.fillColorIndexes.length).forEach((u) => {
u.fillColorIndexes.map((p) => p.index).includes(o.index) && (u.fillColorIndexes = u.fillColorIndexes.filter(
(p) => p.index !== o.index
));
}), a.fillColorIndexes.push({
index: o.index,
distance: h.distance
}), t[o.index].isMostNearer = !0;
}
}), n.filter((a) => a.fillColorIndexes.length).forEach((a) => {
a.fillColorIndexes.forEach((i, h) => {
h === 0 ? t[i.index].hsl = a.hsl : t[i.index].hsl = re({
paletteColors: n.map((f) => f.hsl),
// Useful to generate shade for special colors
paletteColor: a.hsl,
fillColor: t[i.index].hsl
});
});
}), t.map((a) => s(a.hsl).toHexString());
};
export {
ie as colorswap,
ae as grayscale
};