UNPKG

ahex-to-rgba

Version:

AHEX颜色格式转成RGBA格式

31 lines (25 loc) 638 B
module.exports.hexToRgba = h => { return `rgba(${hexToR(h)},${hexToG(h)},${hexToB(h)},${parseFloat(Number(hexToA(h) / 255).toFixed(2))})` } function cutHex(hex) { let temp = 'ffffff' if (hex && hex.charAt(0) === '#') { temp = hex.substring(1, 9) } if (temp.length === 6) { temp = `ff${temp}` } return temp } function hexToA(hex) { return parseInt(cutHex(hex).substring(0, 2), 16) } function hexToR(hex) { return parseInt(cutHex(hex).substring(2, 4), 16) } function hexToG(hex) { return parseInt(cutHex(hex).substring(4, 6), 16) } function hexToB(hex) { return parseInt(cutHex(hex).substring(6, 8), 16) }