题解 | #颜色字符串转换#

颜色字符串转换

https://www.nowcoder.com/practice/80b08802a833419f9c4ccc6e042c1cca

function rgb2hex(sRGB) {
    let tr=sRGB.split('(')[1] ||''
    let tl=tr.split(')')[0] ||''
    let t=tl.split(',')||''
    
    if(t.length!==3)
        return sRGB
    
    
    let res='#'
    
    t.forEach((el)=>{
        let tem=parseInt(el).toString(16)
        res+=tem.length==1?'0'+tem:tem
    })
    return res
}

全部评论
分享了一个不用正则的方法
点赞 回复
分享
发布于 2022-10-06 18:36 江苏

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务