📅  最后修改于: 2022-03-11 15:01:44.635000             🧑  作者: Mango
function rgba2hex(rgba) {
rgba = rgba.match(
/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i
);
return rgba && rgba.length === 4
? "#" +
("0" + parseInt(rgba[1], 10).toString(16)).slice(-2) +
("0" + parseInt(rgba[2], 10).toString(16)).slice(-2) +
("0" + parseInt(rgba[3], 10).toString(16)).slice(-2)
: "";
}
// examples
console.log(rgba2hex('rgba(240, 240, 240, 0.5)'));
console.log(rgba2hex('rgba(40, 20, 80, 1.0)'));