How to get hex color value rather than RGB value?

前端 未结 19 3195
猫巷女王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:33

    Updated @ErickPetru for rgba compatibility:

    function rgb2hex(rgb) {
        rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/);
        function hex(x) {
            return ("0" + parseInt(x).toString(16)).slice(-2);
        }
        return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
    }
    

    I updated the regex to match the alpha value if defined, but not use it.

    0 讨论(0)
  • 2020-11-21 23:33

    Here is a version that also checks for transparent, I needed this since my object was to insert the result into a style attribute, where the transparent version of a hex color is actually the word "transparent"..

    function rgb2hex(rgb) {
         if (  rgb.search("rgb") == -1 ) {
              return rgb;
         }
         else if ( rgb == 'rgba(0, 0, 0, 0)' ) {
             return 'transparent';
         }
         else {
              rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/);
              function hex(x) {
                   return ("0" + parseInt(x).toString(16)).slice(-2);
              }
              return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]); 
         }
    }
    
    0 讨论(0)
  • 2020-11-21 23:33

    This one looks a bit nicer:

    var rgb = $('#selector').css('backgroundColor').match(/\d+/g);
    var r   = parseInt(rgb[0], 10);
    var g   = parseInt(rgb[1], 10);
    var b   = parseInt(rgb[2], 10);
    var hex = '#'+ r.toString(16) + g.toString(16) + b.toString(16);
    

    a more succinct one-liner:

    var rgb = $('#selector').css('backgroundColor').match(/\d+/g);
    var hex = '#'+ Number(rgb[0]).toString(16) + Number(rgb[1]).toString(16) + Number(rgb[2]).toString(16);
    

    forcing jQuery to always return hex:

    $.cssHooks.backgroundColor = {
        get: function(elem) {
            if (elem.currentStyle)
                var bg = elem.currentStyle["backgroundColor"];
            else if (window.getComputedStyle) {
                var bg = document.defaultView.getComputedStyle(elem,
                    null).getPropertyValue("background-color");
            }
            if (bg.search("rgb") == -1) {
                return bg;
            } else {
                bg = bg.match(/\d+/g);
                function hex(x) {
                    return ("0" + parseInt(x).toString(16)).slice(-2);
                }
                return "#" + hex(bg[0]) + hex(bg[1]) + hex(bg[2]);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-21 23:33

    My beautiful non-standard solution

    HTML

    <div id="selector" style="background-color:#f5b405"></div>
    

    jQuery

    $("#selector").attr("style").replace("background-color:", "");
    

    Result

    #f5b405
    
    0 讨论(0)
  • 2020-11-21 23:34

    Here's a solution I found that does not throw scripting errors in IE: http://haacked.com/archive/2009/12/29/convert-rgb-to-hex.aspx

    0 讨论(0)
  • 2020-11-21 23:36

    Steven Pribilinskiy's answer drops leading zeroes, for example #ff0000 becomes #ff00.

    A solution is to append a leading 0 and substring off the last 2 digits.

    var rgb = $('#selector').css('backgroundColor').match(/\d+/g);
    var hex = '#'+ String('0' + Number(rgb[0]).toString(16)).slice(-2) + String('0' + Number(rgb[1]).toString(16)).slice(-2) + String('0' + Number(rgb[2]).toString(16)).slice(-2);
    
    0 讨论(0)
提交回复
热议问题