Js achieve pure color hex to rgb format

 
  // use the slice toString parseInt and converted into hexadecimal color rgb format
        @ Ideas: The hexadecimal color start with an index 1, using the slice method, taken every two
var div1 = document.querySelector("div");

        var color = "#ab0000";

        var str="rgb("

        var r = parseInt (color.slice (1,3), 16) .toString (); // ff slice does not include the 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;

Guess you like

Origin www.cnblogs.com/wanghao1994/p/12099477.html