无意间发现的一段代码,挺有意思:
const hexToRgba = (hex, opacity) => { let rgbaColor = ""; let reg = /^#[da-f]{6}$/i; if (reg.test(hex)) { rgbaColor = `rgba(${parseInt("0x" + hex.slice(1, 3))},${parseInt( "0x" + hex.slice(3, 5) )},${parseInt("0x" + hex.slice(5, 7))},${opacity})`; } return rgbaColor; } // 使用 console.log(hexToRgba("#1B253A", 0.5)) // rgba(27,37,58,0.5)