输入:‘rgb(255, 255, 255)‘
输出:#ffffff
1 function rgb2hex(sRGB) { 2 let re = /rgb\((\d+),\s*(\d+),\s*(\d+)\)/, 3 ret = sRGB.match(re), 4 head = "#"; 5 if(!ret) return sRGB; 6 else { 7 for(let i = 1; i < ret.length; i++) { 8 let x = parseInt(ret[i]); 9 if(x >= 0 && x <= 255) { 10 head += x < 16 ? ‘0‘ + x.toString(16) : x.toString(16); 11 } else return sRGB; 12 } 13 } 14 return head 15 }