将 RGB 值转换为颜色代码。
使用按位左移运算符(< <
)和 toString(16)
将给定的RGB参数转换为十六进制字符串,然后使用 padStart(6,'0')
得到一个6位的十六进制值。
<
pre class=”prettyprint lang-JavaScript linenums:1″>
const RGBToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6, ‘0’);
RGBToHex(255, 165, 1); // 'ffa501'
更多代码 JavaScript 实用代码片段 请查看 https://www.html.cn/30-seconds-of-code/
最新评论
写的挺好的
有没有兴趣翻译 impatient js? https://exploringjs.com/impatient-js/index.html
Flexbox playground is so great!
感谢总结。
awesome!
这个好像很早就看到类似的文章了
比其他的教程好太多了
柯理化讲的好模糊…没懂