nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.
55 lines (54 loc) • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isHex6 = isHex6;
exports.isHex8 = isHex8;
exports.isRGB = isRGB;
exports.isRGBA = isRGBA;
exports.isHSL = isHSL;
exports.isHSLA = isHSLA;
const helpers_1 = require("./helpers");
function isHex6(color) {
return /^#[0-9A-Fa-f]{6}$/.test(color?.trim());
}
function isHex8(color) {
return /^#[0-9A-Fa-f]{8}$/.test(color?.trim());
}
function isRGB(color) {
const match = color
?.trim()
?.match(/^rgb\(\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?)\s*\)$/);
if (!match)
return false;
const [r, g, b] = match.slice(1).map(Number);
return (0, helpers_1._isValidRGBComponent)(r) && (0, helpers_1._isValidRGBComponent)(g) && (0, helpers_1._isValidRGBComponent)(b);
}
function isRGBA(color) {
const match = color
?.trim()
?.match(/^rgba\(\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?),\s*(0|1|0?\.\d+)\s*\)$/);
if (!match)
return false;
const [r, g, b, a] = match.slice(1).map(Number);
return ((0, helpers_1._isValidRGBComponent)(r) &&
(0, helpers_1._isValidRGBComponent)(g) &&
(0, helpers_1._isValidRGBComponent)(b) &&
(0, helpers_1._isValidAlpha)(a));
}
function isHSL(color) {
const match = color
?.trim()
?.match(/^hsl\(\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?)%,\s*(\d{1,3}(?:\.\d+)?)%\s*\)$/);
if (!match)
return false;
const [h, s, l] = match.slice(1).map(Number);
return (0, helpers_1._isValidHue)(h) && (0, helpers_1._isValidPercentage)(s) && (0, helpers_1._isValidPercentage)(l);
}
function isHSLA(color) {
const match = color
?.trim()
.match(/^hsla\(\s*(\d{1,3}(?:\.\d+)?),\s*(\d{1,3}(?:\.\d+)?)%,\s*(\d{1,3}(?:\.\d+)?)%,\s*(0|1|0?\.\d+)\s*\)$/);
if (!match)
return false;
const [h, s, l, a] = match.slice(1).map(Number);
return (0, helpers_1._isValidHue)(h) && (0, helpers_1._isValidPercentage)(s) && (0, helpers_1._isValidPercentage)(l) && (0, helpers_1._isValidAlpha)(a);
}