题解 | #颜色字符串转换#
颜色字符串转换
http://www.nowcoder.com/practice/80b08802a833419f9c4ccc6e042c1cca
挑战优雅写法
function rgb2hex(sRGB) { if (!/^rgb\((\d{1,3},\s*){2}\d{1,3}\)$/.test(sRGB)) return sRGB; let color = '#'; sRGB.replace(/\d+/g, n => color += ('0' + (+n).toString(16)).slice(-2)); return color; }