@nauverse/color-to-hsla
Advanced tools
Comparing version 1.12.1 to 1.13.0
@@ -1,2 +0,2 @@ | ||
const R = /^#([0-9a-f]{3})$/, x = /^#([0-9a-f]{6})$/, F = /^rgb\(\s*([-+]?\d+)\s*,\s*([-+]?\d+)\s*,\s*([-+]?\d+)\s*\)$/, I = /^rgb\(\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*\)$/, H = /^rgba\(\s*([-+]?\d+)\s*,\s*([-+]?\d+)\s*,\s*([-+]?\d+)\s*,\s*([-+]?[\d\.]+)\s*\)$/, m = /^rgba\(\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?[\d\.]+)\s*\)$/, f = /^hsl\(\s*([-+]?\d+(?:\.\d+)?)\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*\)$/, q = /^hsla\(\s*([-+]?\d+(?:\.\d+)?)\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?[\d\.]+)\s*\)$/, b = { | ||
const x = /^#([0-9a-f]{3})$/, F = /^#([0-9a-f]{6})$/, m = /^rgb\(\s*([-+]?\d+)\s*,\s*([-+]?\d+)\s*,\s*([-+]?\d+)\s*\)$/, R = /^rgb\(\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*\)$/, I = /^rgba\(\s*([-+]?\d+)\s*,\s*([-+]?\d+)\s*,\s*([-+]?\d+)\s*,\s*([-+]?[\d\.]+)\s*\)$/, M = /^rgba\(\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?[\d\.]+)\s*\)$/, $ = /^hsl\(\s*([-+]?\d+(?:\.\d+)?)\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*\)$/, H = /^hsla\(\s*([-+]?\d+(?:\.\d+)?)\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?\d+(?:\.\d+)?)%\s*,\s*([-+]?[\d\.]+)\s*\)$/, b = { | ||
aliceblue: 15792383, | ||
@@ -151,73 +151,73 @@ antiquewhite: 16444375, | ||
}, y = { h: 0, s: 0, l: 0, a: 0 }; | ||
function P(e) { | ||
function q(e) { | ||
if (typeof e == "string") | ||
return $(e); | ||
return f(e); | ||
if (typeof e != "object") | ||
throw new Error("Must pass string or object"); | ||
const r = e.a == null ? 1 : e.a; | ||
const s = e.a == null ? 1 : e.a; | ||
if (k(e, "hsl")) | ||
return p((e == null ? void 0 : e.h) ?? 0, (e == null ? void 0 : e.s) ?? 0, (e == null ? void 0 : e.l) ?? 0, r); | ||
return p((e == null ? void 0 : e.h) ?? 0, (e == null ? void 0 : e.s) ?? 0, (e == null ? void 0 : e.l) ?? 0, s); | ||
if (k(e, "rgb")) | ||
return h((e == null ? void 0 : e.r) ?? 0, (e == null ? void 0 : e.g) ?? 0, (e == null ? void 0 : e.b) ?? 0, r); | ||
return h((e == null ? void 0 : e.r) ?? 0, (e == null ? void 0 : e.g) ?? 0, (e == null ? void 0 : e.b) ?? 0, s); | ||
throw new Error("Could not parse argument"); | ||
} | ||
function k(e, r) { | ||
const t = r.split(""); | ||
function k(e, s) { | ||
const r = s.split(""); | ||
let o = !1; | ||
for (let i = 0; i < t.length; i++) { | ||
const a = t[i]; | ||
for (let i = 0; i < r.length; i++) { | ||
const a = r[i]; | ||
if (typeof a < "u" && a in e) | ||
o = !0; | ||
else if (typeof a > "u" && o) | ||
throw new Error('Missing key "' + a + '" in color type ' + r); | ||
throw new Error('Missing key "' + a + '" in color type ' + s); | ||
} | ||
return o; | ||
} | ||
function $(e) { | ||
function f(e) { | ||
try { | ||
e = `${e}`.trim().toLowerCase(); | ||
const r = f.exec(e); | ||
const s = $.exec(e); | ||
if (s) { | ||
const n = s.length >= 2 ? Math.round(parseInt(s[1] ?? "0", 10)) : 0, l = s.length >= 3 ? parseFloat(s[2] ?? "0") : 0, t = s.length >= 4 ? parseFloat(s[3] ?? "0") : 0; | ||
return p(n, l / 100, t / 100, 1); | ||
} | ||
const r = H.exec(e); | ||
if (r) { | ||
const s = r.length >= 2 ? Math.round(parseInt(r[1] ?? "0", 10)) : 0, l = r.length >= 3 ? parseFloat(r[2] ?? "0") : 0, n = r.length >= 4 ? parseFloat(r[3] ?? "0") : 0; | ||
return p(s, l / 100, n / 100, 1); | ||
const n = r.length >= 2 ? Math.round(parseInt(r[1] ?? "0", 10)) : 0, l = r.length >= 3 ? parseFloat(r[2] ?? "0") : 0, t = r.length >= 4 ? parseFloat(r[3] ?? "0") : 0, c = r.length >= 5 ? parseFloat(r[4] ?? "0") : 0; | ||
return p(n, l / 100, t / 100, c); | ||
} | ||
const t = q.exec(e); | ||
if (t) { | ||
const s = t.length >= 2 ? Math.round(parseInt(t[1] ?? "0", 10)) : 0, l = t.length >= 3 ? parseFloat(t[2] ?? "0") : 0, n = t.length >= 4 ? parseFloat(t[3] ?? "0") : 0, c = t.length >= 5 ? parseFloat(t[4] ?? "0") : 0; | ||
return p(s, l / 100, n / 100, c); | ||
} | ||
const o = R.exec(e); | ||
const o = x.exec(e); | ||
if (o) { | ||
const s = parseInt(o[1] ?? "0", 16); | ||
const n = parseInt(o[1] ?? "0", 16); | ||
return h( | ||
s >> 8 & 15 | s >> 4 & 240, | ||
s >> 4 & 15 | s & 240, | ||
(s & 15) << 4 | s & 15, | ||
n >> 8 & 15 | n >> 4 & 240, | ||
n >> 4 & 15 | n & 240, | ||
(n & 15) << 4 | n & 15, | ||
1 | ||
); | ||
} | ||
const i = x.exec(e); | ||
const i = F.exec(e); | ||
if (i) { | ||
const s = parseInt(i[1] ?? "0", 16); | ||
return v(s); | ||
const n = parseInt(i[1] ?? "0", 16); | ||
return v(n); | ||
} | ||
const a = F.exec(e); | ||
const a = m.exec(e); | ||
if (a) { | ||
const s = parseInt(a[1] ?? "0", 10), l = parseInt(a[2] ?? "0", 10), n = parseInt(a[3] ?? "0", 10); | ||
return h(s, l, n, 1); | ||
const n = parseInt(a[1] ?? "0", 10), l = parseInt(a[2] ?? "0", 10), t = parseInt(a[3] ?? "0", 10); | ||
return h(n, l, t, 1); | ||
} | ||
const d = I.exec(e); | ||
const d = R.exec(e); | ||
if (d) { | ||
const s = parseFloat(d[1] ?? "0") * 255 / 100, l = parseFloat(d[2] ?? "0") * 255 / 100, n = parseFloat(d[3] ?? "0") * 255 / 100; | ||
return h(s, l, n, 1); | ||
const n = parseFloat(d[1] ?? "0") * 255 / 100, l = parseFloat(d[2] ?? "0") * 255 / 100, t = parseFloat(d[3] ?? "0") * 255 / 100; | ||
return h(n, l, t, 1); | ||
} | ||
const g = H.exec(e); | ||
const g = I.exec(e); | ||
if (g) { | ||
const s = parseInt(g[1] ?? "0", 10), l = parseInt(g[2] ?? "0", 10), n = parseInt(g[3] ?? "0", 10), c = parseFloat(g[4] ?? "0"); | ||
return w(s, l, n, c); | ||
const n = parseInt(g[1] ?? "0", 10), l = parseInt(g[2] ?? "0", 10), t = parseInt(g[3] ?? "0", 10), c = parseFloat(g[4] ?? "0"); | ||
return w(n, l, t, c); | ||
} | ||
const u = m.exec(e); | ||
const u = M.exec(e); | ||
if (u) { | ||
const s = parseFloat(u[1] ?? "0") * 255 / 100, l = parseFloat(u[2] ?? "0") * 255 / 100, n = parseFloat(u[3] ?? "0") * 255 / 100, c = parseFloat(u[4] ?? "0"); | ||
return w(s, l, n, c); | ||
const n = parseFloat(u[1] ?? "0") * 255 / 100, l = parseFloat(u[2] ?? "0") * 255 / 100, t = parseFloat(u[3] ?? "0") * 255 / 100, c = parseFloat(u[4] ?? "0"); | ||
return w(n, l, t, c); | ||
} | ||
@@ -232,17 +232,22 @@ return b[e] ? v(b[e]) : e === "transparent" ? h(0, 0, 0, 0) : y; | ||
} | ||
function w(e, r, t, o) { | ||
return h(e, r, t, o); | ||
function w(e, s, r, o) { | ||
return h(e, s, r, o); | ||
} | ||
function h(e, r, t, o) { | ||
const i = +e / 255, a = +r / 255, d = +t / 255, g = +o, u = Math.min(i, a, d), s = Math.max(i, a, d); | ||
let l = 0, n = s - u; | ||
const c = (s + u) / 2; | ||
return n ? (i === s ? l = (a - d) / n + (a < d ? 1 : 0) * 6 : a === s ? l = (d - i) / n + 2 : l = (i - a) / n + 4, n /= c < 0.5 ? s + u : 2 - s - u, l *= 60) : n = c > 0 && c < 1 ? 0 : l, { h: Math.round(l), s: isNaN(n) ? 0 : n, l: isNaN(c) ? 0 : c, a: g }; | ||
function h(e, s, r, o) { | ||
const i = +e / 255, a = +s / 255, d = +r / 255, g = +o, u = Math.min(i, a, d), n = Math.max(i, a, d); | ||
let l = 0, t = n - u; | ||
const c = (n + u) / 2; | ||
return t ? (i === n ? l = (a - d) / t + (a < d ? 1 : 0) * 6 : a === n ? l = (d - i) / t + 2 : l = (i - a) / t + 4, t /= c < 0.5 ? n + u : 2 - n - u, l *= 60) : t = c > 0 && c < 1 ? 0 : l, { h: Math.round(l), s: isNaN(t) ? 0 : t, l: isNaN(c) ? 0 : c, a: g }; | ||
} | ||
function p(e, r, t, o) { | ||
return { h: +e, s: +r, l: +t, a: +o }; | ||
function p(e, s, r, o) { | ||
return { h: +e, s: +s, l: +r, a: +o }; | ||
} | ||
function P(e) { | ||
const s = Math.round(Math.max(0, Math.min(360, e.h))).toFixed(0), r = (Math.max(0, Math.min(1, e.s)) * 100).toFixed(0), o = (Math.max(0, Math.min(1, e.l)) * 100).toFixed(0), i = parseFloat(e.a.toFixed(1)); | ||
return `hsla(${s}, ${r}%, ${o}%, ${i})`; | ||
} | ||
export { | ||
P as colorToHSLA | ||
q as colorToHSLA, | ||
P as hslaToString | ||
}; | ||
//# sourceMappingURL=color-to-hsla.js.map |
/** | ||
* Parse any valid color string or color object and return HSLA values | ||
* {Object|String} format | ||
* Converts a color value to HSLA format. | ||
* | ||
* @param format - The color value to convert. It can be either a string or an object. | ||
* @returns The color value in HSLA format. | ||
* @throws Error if the format is not a string or an object, or if the argument cannot be parsed. | ||
*/ | ||
@@ -14,2 +17,9 @@ export declare function colorToHSLA(format: string | IColorObject): HSLA; | ||
/** | ||
* Converts an HSLA color object to a string representation. | ||
* @param hsla - The HSLA color object to convert. | ||
* @returns The string representation of the HSLA color. | ||
*/ | ||
export declare function hslaToString(hsla: HSLA): string; | ||
declare interface IColorObject { | ||
@@ -16,0 +26,0 @@ h?: number; |
@@ -33,3 +33,3 @@ { | ||
], | ||
"version": "1.12.1", | ||
"version": "1.13.0", | ||
"type": "module", | ||
@@ -36,0 +36,0 @@ "main": "./dist/color-to-hsla.umd.cjs", |
@@ -198,2 +198,26 @@ <h1 align="center"> | ||
There is another function, `hslaToString`, which allows you to convert any HSLA object to a valid CSS string: | ||
~~~ts | ||
import { hslaToString } from "@nauverse/color-to-hsla"; | ||
const myHSLAString = hslaToString({ | ||
h: 120, | ||
s: 0.5, | ||
l: 0.5, | ||
a: 0.1 | ||
}); // "hsla(120, 50%, 50%, 0.1)" | ||
~~~ | ||
~~~ts | ||
import { hslaToString } from "@nauverse/color-to-hsla"; | ||
const myHSLAString = hslaToString({ | ||
h: 120, | ||
s: 0.5, | ||
l: 0.5, | ||
a: 1 | ||
}); // "hsla(120, 50%, 50%, 1)" | ||
~~~ | ||
## Help | ||
@@ -200,0 +224,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
72128
308
292