How can I compare two color values in jQuery/JavaScript?

后端 未结 6 1874
耶瑟儿~
耶瑟儿~ 2020-12-06 16:05

I get color value with jQuery with .css(\'color\'), and then I know color that it should be.

How can I compare color value that I get from jQuery with

6条回答
  •  情书的邮戳
    2020-12-06 16:49

    Actually I tried Charlie Kilian's answer. For some reason it didn't work when you try to set .css('color') with and 'rgb(0,0,0)' value.

    I don't know why. Worked perfectly in the console. Maybe it was because my comparing function is in a javascript object and its some kind of a context issue or a reference problem. Either way finally I got frustrated and wrote my own function that will compare two colors regardless of the formats and does not create any elements or rely on jQuery. The function takes both HEX and RGB values.

    It can probably be optimized but I really don't have the time right now. Hope this helps someone its pure javascript.

    var compareColors= function (left, right) {
           var l= parseColor(left);
            var r=parseColor(right);
            if(l !=null && r!=null){
               return l.r == r.r && l.g == r.g && l.b == r.b;
            }else{
                return false;
            }
            function parseColor(color){
               if(color.length==6 || color.length==7){
                    //hex color
                   return {
                        r:hexToR(color),
                        g:hexToG(color),
                        b:hexToB(color)
                    }
                }else if(color.toLowerCase().indexOf('rgb')>-1){
                  var arr
                    var re = /\s*[0-9]{1,3}\s*,\s*[0-9]{1,3}\s*,\s*[0-9]{1,3}\s*/gmi;
                    var m;
    
                    if ((m = re.exec(color)) !== null) {
                        if (m.index === re.lastIndex) {
                            re.lastIndex++;
                        }
                        // View your result using the m-variable.
                        // eg m[0] etc.
                        arr = m[0].split(',');
                        return {
                            r: parseInt(arr[0].trim()),
                            g: parseInt(arr[1].trim()),
                            b: parseInt(arr[2].trim())
                        }
                    }else{
                      return null;
                    }
    
                } else{
                    return null;
                }
                function hexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
                function hexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
                function hexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
                function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}
            }
        }
    

    These following functions I took from www.javascripter.net

                function hexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
                function hexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
                function hexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
                function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}
    

提交回复
热议问题