UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.

37 lines (36 loc) 1.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractAlphaColorValues = exports.extractSolidColorValues = void 0; const helpers_1 = require("./helpers"); /** * * Extracts numbers from a color string like `rgb(66, 103, 69)` or `hsl(120, 42.86%, 41.18%)`. * * Converts percentage values to decimal (e.g., `42.86%` → `42.86`). * * @param color The color string in RGB or HSL format. * @returns A tuple of 3 extracted numbers. `[number, number, number]` * * @remarks If the input color is not in `HSL` or `RGB` format, it will return `[0, 0, 0]` */ const extractSolidColorValues = (color) => { if ((0, helpers_1._isHSL)(color) || (0, helpers_1._isRGB)(color)) { return (color?.trim()?.match(/[\d.]+%?/g) || [])?.map((value) => parseFloat(value)); } return [0, 0, 0]; }; exports.extractSolidColorValues = extractSolidColorValues; /** * * Extracts numbers from a color string like `rgba(66, 103, 69, 0.6)` or `hsla(120, 42.86%, 41.18%, 0.9)`. * * Converts percentage values to decimal (e.g., `42.86%` → `42.86`). * * @param color The color string in RGBA or HSLA format. * @returns A tuple of 4 extracted numbers. `[number, number, number, number]` * * @remarks If the input color is not in `HSLA` or `RGBA` format, it will return `[0, 0, 0, 0]` */ const extractAlphaColorValues = (color) => { if ((0, helpers_1._isHSLA)(color) || (0, helpers_1._isRGBA)(color)) { return (color?.trim()?.match(/[\d.]+%?/g) || [])?.map((value) => parseFloat(value)); } return [0, 0, 0, 0]; }; exports.extractAlphaColorValues = extractAlphaColorValues;