How to get hex color value rather than RGB value?

前端 未结 19 3151
猫巷女王i
猫巷女王i 2020-11-21 23:06

Using the following jQuery will get the RGB value of an element\'s background color:

$(\'#selector\').css(\'backgroundColor\');

Is there a

19条回答
  •  一向
    一向 (楼主)
    2020-11-21 23:32

    Try

    // c - color str e.g."rgb(12,233,43)", result color hex e.g. "#0ce92b"
    let rgb2hex= c=> '#'+c.match(/\d+/g).map(x=>(+x).toString(16).padStart(2,0)).join``
    

    // rgb - color str e.g."rgb(12,233,43)", result color hex e.g. "#0ce92b"
    let rgb2hex= c=> '#'+c.match(/\d+/g).map(x=>(+x).toString(16).padStart(2,0)).join``
    
    console.log(rgb2hex("rgb(12,233,43"));

提交回复
热议问题