rgb转hex.js 425 B

1234567891011121314151617181920
  1. /*
  2. * @Description: rgb值转换hex
  3. * @Autor: kindring
  4. * @Date: 2021-11-16 11:21:02
  5. * @LastEditors: kindring
  6. * @LastEditTime: 2022-01-06 11:38:33
  7. * @LastDescript:
  8. */
  9. let rgb = 'rgb(244,246,244)'
  10. function rgbToHex(str) {
  11. return str.replace(/(rgb\(|\))/g, '').split(',').reduce((a, c) => {
  12. c = parseInt(c).toString(16)
  13. return a + `${ c.length<2?'0'+c:c}`
  14. }, '#')
  15. }
  16. console.log(rgbToHex(rgb));