function rgb2hex(sRGB) {
const reg = /^rgb\((\s*\d{1,3},?){3}\)$/;
if (!reg.test(sRGB)) {
return sRGB;
}
let result = '#';
const reg2 = /^rgb\(((\s*\d{1,3},?){3})\)$/;
sRGB.replace(reg2, (match, p1) => {
p1.split(',').forEach(item => {
let itemStr = (+item).toString(16);
// 如果支持parStart方法,可以这样写:itemStr = itemStr.padStart(2, '0');
if (itemStr.length === 1) {
itemStr = '0' + itemStr;
}
result += itemStr;
});
});
return result;
const reg = /^rgb\((\s*\d{1,3},?){3}\)$/;
if (!reg.test(sRGB)) {
return sRGB;
}
let result = '#';
const reg2 = /^rgb\(((\s*\d{1,3},?){3})\)$/;
sRGB.replace(reg2, (match, p1) => {
p1.split(',').forEach(item => {
let itemStr = (+item).toString(16);
// 如果支持parStart方法,可以这样写:itemStr = itemStr.padStart(2, '0');
if (itemStr.length === 1) {
itemStr = '0' + itemStr;
}
result += itemStr;
});
});
return result;
}