Get hex value of clicked on color with jQuery

前端 未结 3 438
孤独总比滥情好
孤独总比滥情好 2021-01-07 00:37

I want to know how to make a color picker with jQuery that will allow you to click somewhere on the page and return the hex color value of the color that you clicked on.

3条回答
  •  佛祖请我去吃肉
    2021-01-07 01:02

    I had this problem recently, by default IE returns hex colours whereas all the good browsers return rgb values, I simply put conditions to deal with both strings, this would be more efficient..

    however I think this function does the trick if you really need it

    function RGBToHex(rgb) { 
    var char = "0123456789ABCDEF"; 
    return String(char.charAt(Math.floor(rgb / 16))) + String(char.charAt(rgb - (Math.floor(rgb / 16) * 16))); 
    } 
    

提交回复
热议问题