RGB to hex and hex to RGB

前端 未结 30 2778
遥遥无期
遥遥无期 2020-11-21 06:56

How to convert colors in RGB format to hex format and vice versa?

For example, convert \'#0080C0\' to (0, 128, 192).

30条回答
  •  迷失自我
    2020-11-21 07:29

    For convert directly from jQuery you can try:

      function rgbToHex(color) {
        var bg = color.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
        function hex(x) {
          return ("0" + parseInt(x).toString(16)).slice(-2);
        }
        return     "#" + hex(bg[1]) + hex(bg[2]) + hex(bg[3]);
      }
    
      rgbToHex($('.col-tab-bar .col-tab span').css('color'))
    

提交回复
热议问题