js rgb和16进制相互转换
1.rgb to 16进制
function colorRGBtoHex(color) { var rgb = color.split(','); var r = parseInt(rgb[0]); var g = parseInt(rgb[1]); var b = parseInt(rgb[2]); var hex = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1); return hex; }
2. 16进制 to rgb
function hex2rgb(a) { if (a == ""){ return "" } a=a.substring(1); a=a.toLowerCase(); b=new Array(); for(x=0;x<3;x++){ b[0]=a.substr(x*2,2); b[3]="0123456789abcdef"; b[1]=b[0].substr(0,1); b[2]=b[0].substr(1,1); b[20+x]=b[3].indexOf(b[1])*16+b[3].indexOf(b[2]); } return "rgb(" + b[20]+","+b[21]+","+b[22] + ")"; }
欢迎指正,交流沟通,共同进步!对您有帮助的话点下推荐~~