UNPKG

nhb-toolbox

Version:

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

32 lines (31 loc) 1.33 kB
import { _isHSL, _isHSLA, _isRGB, _isRGBA } from './helpers.js'; /** * * 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]` */ export const extractSolidColorValues = (color) => { if (_isHSL(color) || _isRGB(color)) { return (color?.trim()?.match(/[\d.]+%?/g) || [])?.map((value) => parseFloat(value)); } return [0, 0, 0]; }; /** * * 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]` */ export const extractAlphaColorValues = (color) => { if (_isHSLA(color) || _isRGBA(color)) { return (color?.trim()?.match(/[\d.]+%?/g) || [])?.map((value) => parseFloat(value)); } return [0, 0, 0, 0]; };