// 利用parseInt 和 slice toString将16进制颜色转成rgb格式
// 思路:将16进制的颜色从下标1开始,利用slice方法,每隔两位取出
var div1 = document.querySelector("div");
var color = "#ab0000";
var str="rgb("
var r = parseInt(color.slice(1,3),16).toString(); //ff slice不包括end
var g = parseInt(color.slice(3,5),16).toString(); //00
var b = parseInt(color.slice(5,7),16).toString(); //ff
str += r+","+g+","+b+")";
console.log(str); //rgb(171,0,0)
div1.style.width = 1+"rem";
div1.style.height = 1+"rem";
div1.style.backgroundColor = str;
来源:https://www.cnblogs.com/wanghao1994/p/12099477.html