@ryanuo/utils
Version:
提供多种实用工具函数,涵盖算法、浏览器操作、网络请求等多个领域
46 lines (45 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hexToRgba = hexToRgba;
exports.lerpColor = lerpColor;
exports.rgbaToHex = rgbaToHex;
function hexToRgba(hex) {
if (!/^#(?:[0-9a-f]{6}|[0-9a-f]{8})$/i.test(hex)) throw new Error("Invalid hex color");
const r = Number.parseInt(hex.slice(1, 3), 16);
const g = Number.parseInt(hex.slice(3, 5), 16);
const b = Number.parseInt(hex.slice(5, 7), 16);
const a = hex.length > 7 ? Number.parseInt(hex.slice(7, 9), 16) / 255 : 1;
return {
r,
g,
b,
a
};
}
function rgbaToHex({
r,
g,
b,
a = 1
}) {
if (r < 0 || r > 255) throw new Error("Invalid red value");
if (g < 0 || g > 255) throw new Error("Invalid green value");
if (b < 0 || b > 255) throw new Error("Invalid blue value");
if (a < 0 || a > 1) throw new Error("Invalid alpha value");
const toHex = n => Math.round(n).toString(16).padStart(2, "0");
return `#${toHex(r)}${toHex(g)}${toHex(b)}${a < 1 ? toHex(a * 255) : ""}`;
}
function lerpColor(color1, color2, t) {
if (!/^#(?:[0-9a-f]{6}|[0-9a-f]{8})$/i.test(color1)) throw new Error("Invalid hex color");
if (t < 0 || t > 1) throw new Error("Invalid t value");
const c1 = hexToRgba(color1);
const c2 = hexToRgba(color2);
return rgbaToHex({
r: c1.r + (c2.r - c1.r) * t,
g: c1.g + (c2.g - c1.g) * t,
b: c1.b + (c2.b - c1.b) * t,
a: c1.a + (c2.a - c1.a) * t
});
}