UNPKG

@brizy/ui

Version:
27 lines (26 loc) 1.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hexToRgb = exports.hexToRgba = void 0; const Hex_1 = require("../types/Hex"); const hexToRgba = (hex, opacity) => { if ((0, Hex_1.isHex)(hex)) { hex = hex.replace("#", ""); opacity = !isNaN(opacity) ? opacity : 1; const r = parseInt(hex.substring(0, 2), 16); const g = parseInt(hex.substring(2, 4), 16); const b = parseInt(hex.substring(4, 6), 16); return `rgba(${r}, ${g}, ${b}, ${opacity})`; } return undefined; }; exports.hexToRgba = hexToRgba; const hexToRgb = (hex) => { // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF") const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; hex = hex.replace(shorthandRegex, (_m, r, g, b) => { return r + r + g + g + b + b; }); const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); return result ? [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)] : undefined; }; exports.hexToRgb = hexToRgb;