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

颜色字符串转换

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

{"css":"","js":"function rgb2hex(sRGB) {\n    const reg = /^\\s*rgb\\s*\\(\\s*\\d{1,3}\\s*,\\s*\\d{1,3}\\s*,\\s*\\d{1,3}\\s*\\)\\s*$/\n    if(!reg.test(sRGB)){\n        return sRGB;\n    }\n    let arr = sRGB.split(/[\\(\\)]/)[1].split(',')\n    let ret = '#'\n    arr.forEach((item,index)=>{\n    \td=parseInt(item)\n    \tif(d<0 && d>255){\n    \t\treturn sRGB\n    \t}\n    \tlet h=d.toString(16)\n    \tret += (h.length===1?'0'+h:h)\n    })\n    console.log(ret)\n    return ret\n}\n\n\nrgb2hex('rgb(5,5,   5)')","html":"","libs":[]}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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