function rgb2hex(sRGB) { // 去除 sRGB 的空格 const newSRGB = sRGB.replace(/\s+/g,""); // 定义 rbg 的正则表达式 const exp = /rgb\((0|1\d{0,2}|2[0-5]{2}),(0|1\d{0,2}|2[0-5]{2}),(0|1\d{0,2}|2[0-5]{2})\)/; // 若不符合规则,则返回原字符串 if (!exp.test(newSRGB)) return sRGB // 剔除 'rgb(' 和 ')', 剩下 'xx,xx,xx' 的字符串 const rgbString = newSRGB.substring(4, newSRGB.length - 1) // 拆分上述字符串,生成数组 const arr = rgbString.split(',') let color = '#' // 遍历数组,将每个元素转成16进制的字符串 for (const item of arr) { console.log(item) const str = Number(item).toString(16) color += str.length === 2 ? str : `0${str}` } return color }