function RGB2Hex(rgb){ var re = rgb.replace(/(?:\(|\)|rgb|RGB)*/g,"").split(",");//利用正則表達式去掉多餘的部分 var hexColor = "#"; var hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']; for (var i = 0; i < 3; i++) { var r = null; var c = re[i]; var hexAr = []; while (c > 16) { r = c % 16; c = (c / 16) >> 0; hexAr.push(hex[r]); } hexAr.push(hex[c]); hexColor += hexAr.reverse().join(''); } return hexColor; }