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

前端 未结 8 1626
悲哀的现实
悲哀的现实 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:15

    Interesting question. My immediate thought was to invert the color of the background as the text. This involves simply parsing the background and inverting its RGB value.

    Something like this: http://jsfiddle.net/2VTnZ/2/

    var rgb = $('#test').css('backgroundColor');
    var colors = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    var brightness = 1;
    
    var r = colors[1];
    var g = colors[2];
    var b = colors[3];
    
    var ir = Math.floor((255-r)*brightness);
    var ig = Math.floor((255-g)*brightness);
    var ib = Math.floor((255-b)*brightness);
    
    $('#test').css('color', 'rgb('+ir+','+ig+','+ib+')');
    

提交回复
热议问题