RGB to hex and hex to RGB

前端 未结 30 2780
遥遥无期
遥遥无期 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:04

    May you be after something like this?

    function RGB2HTML(red, green, blue)
    {
        return '#' + red.toString(16) +
               green.toString(16) +
               blue.toString(16);
    }
    
    alert(RGB2HTML(150, 135, 200));
    

    displays #9687c8

提交回复
热议问题