Change text color based on brightness of the covered background area?

前端 未结 8 1629
悲哀的现实
悲哀的现实 2020-11-27 09:53

I\'ve thought about the following for a while already, so now I want to know your opinions, possible solutions, and so on.

I am looking for a plugin or technique tha

8条回答
  •  广开言路
    2020-11-27 10:05

    By combining the answers [ @alex-ball , @jeremyharris ] I found this to be the best way for me:

            $('.elzahaby-bg').each(function () {
                var rgb = $(this).css('backgroundColor');
                var colors = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    
                var r = colors[1];
                var g = colors[2];
                var b = colors[3];
    
                var o = Math.round(((parseInt(r) * 299) + (parseInt(g) * 587) + (parseInt(b) * 114)) /1000);
    
                if(o > 125) {
                    $(this).css('color', 'black');
                }else{
                    $(this).css('color', 'white');
                }
            });
    *{
        padding: 9px;
    }
    
    
    color is white
    color is black
    color is black
    color is white

提交回复
热议问题