RGB和十六进制转换

时间:2024-02-17 18:21:59

1.十六进制换RGB

例:  var color = \'#69ad52\'

let r = parseInt(“0px” + color.slice(1, 3))  //105

let g = parseInt(“0px” + color.slice(3, 5)) //173

let b = parseInt(“0px” + color.slice(5, 7)) //82

 

2.RGB转十六进制

例:  var color = \'rgb(105, 173,82)\'

let hex = "#" + (( 1<< 24 ) + ( r << 16 ) + ( g << 8 ) + b).toString(16).slice(1)  // #69ad52