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

颜色字符串转换

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('')}`
}
全部评论

相关推荐

03-04 07:14
门头沟学院 C++
何木健一:去啥?你能考虑去就是思想有问题,当然一周到岗一天可以考虑一下😨
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务