题解 | #颜色字符串转换#
颜色字符串转换
http://www.nowcoder.com/practice/80b08802a833419f9c4ccc6e042c1cca
function rgb2hex(sRGB) {
let rgb = sRGB.replace(/[\s*|rgb\(|\)]/gi, '').split(',')
let hexCode = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
for(let i=0,imax=rgb.length; i<imax;i++) {
if(isNaN(+rgb[i])){
return sRGB
}
let v = +rgb[i]
if (v > 255) {
return sRGB
}
rgb[i] = `${hexCode[parseInt(v/16)]}${hexCode[parseInt(v%16)]}`
}
return `#${rgb.join('')}`
}
拼多多集团-PDD公司氛围 754人发布
查看18道真题和解析