题解 | #颜色字符串转换#
颜色字符串转换
http://www.nowcoder.com/practice/80b08802a833419f9c4ccc6e042c1cca
function rgb2hex(sRGB) {
let tempR=sRGB.split('(')[1]||'';
let tempL=tempR.split(')')[0]||'';
let temp=tempL.split(',')||'';
if(temp.length!=3){
return sRGB
}
let res='#';
temp.forEach(item=>{
res+=getNum(parseInt(item))
})
return res;
function getNum(n){
if(Math.floor(n/16)==0){
let temp=n%16>9? String.fromCharCode(97+n%16-10):n%16
if(temp+''.length<=1){
temp=0+''+temp;
}
return temp
}else{
return getNum(Math.floor(n/16))+''+ (n%16>9? String.fromCharCode(97+n%16-10):n%16)
}
}
}
console.log(rgb2hex('abcdefg'))