UNPKG

nhb-toolbox

Version:

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

40 lines (39 loc) 1.65 kB
export const _applyOpacity = (color, opacity) => { return color?.slice(0, 7).concat(opacity); }; export const _generateRandomHSL = () => { const hue = Math.floor(Math.random() * 360); const saturation = 75 + Math.floor(Math.random() * 25); const lightness = 50 + Math.floor(Math.random() * 15); return `hsl(${hue}, ${saturation}%, ${lightness}%)`; }; export const _isSimilarToLast = (recentColors, newColor) => { if (recentColors?.length === 0) return false; const newHSL = newColor.match(/hsl\((\d+), (\d+)%, (\d+)%\)/); const lastHSL = recentColors[recentColors?.length - 1].match(/hsl\((\d+), (\d+)%, (\d+)%\)/); if (!newHSL || !lastHSL) return false; const newHue = parseInt(newHSL[1], 10); const newSaturation = parseInt(newHSL[2], 10); const newLightness = parseInt(newHSL[3], 10); const lastHue = parseInt(lastHSL[1], 10); const lastSaturation = parseInt(lastHSL[2], 10); const lastLightness = parseInt(lastHSL[3], 10); const hueDifference = Math.abs(newHue - lastHue); const saturationDifference = Math.abs(newSaturation - lastSaturation); const lightnessDifference = Math.abs(newLightness - lastLightness); return hueDifference < 48 && saturationDifference < 24 && lightnessDifference < 16; }; export function _isValidAlpha(value) { return value >= 0 && value <= 1 && !Number.isNaN(value); } export function _isValidRGBComponent(value) { return value >= 0 && value <= 255; } export function _isValidHue(value) { return value >= 0 && value <= 360; } export function _isValidPercentage(value) { return value >= 0 && value <= 100; }