UNPKG

@mapcss/preset-svg

Version:
37 lines (36 loc) 1.11 kB
export function hex2RGBA(str) { const [, body] = str.match(/^#?([\da-f]+)$/i) || []; if (!body) { return; } switch (body.length) { case 3: case 4: { const digits = Array.from(body, (s) => Number.parseInt(s, 16)).map((n) => (n << 4) | n); const [r, g, b] = digits; return { r, g, b, a: body.length === 3 ? undefined : Math.round(digits[3] / 255 * 100) / 100, }; } case 6: case 8: { const value = Number.parseInt(body, 16); const [r, g, b] = body.length === 6 ? [(value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF] : [(value >> 24) & 0xFF, (value >> 16) & 0xFF, (value >> 8) & 0xFF]; return { r, g, b, a: body.length === 6 ? undefined : Math.round((value & 0xFF) / 255 * 100) / 100, }; } } }