UNPKG

nhb-toolbox

Version:

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

50 lines (49 loc) 2.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._isSimilarToLast = exports._generateRandomHSL = exports._applyOpacity = void 0; exports._isValidAlpha = _isValidAlpha; exports._isValidRGBComponent = _isValidRGBComponent; exports._isValidHue = _isValidHue; exports._isValidPercentage = _isValidPercentage; const _applyOpacity = (color, opacity) => { return color?.slice(0, 7).concat(opacity); }; exports._applyOpacity = _applyOpacity; 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}%)`; }; exports._generateRandomHSL = _generateRandomHSL; 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; }; exports._isSimilarToLast = _isSimilarToLast; function _isValidAlpha(value) { return value >= 0 && value <= 1 && !Number.isNaN(value); } function _isValidRGBComponent(value) { return value >= 0 && value <= 255; } function _isValidHue(value) { return value >= 0 && value <= 360; } function _isValidPercentage(value) { return value >= 0 && value <= 100; }