Automatically change text color to assure readability

前端 未结 8 2047
生来不讨喜
生来不讨喜 2020-12-08 00:42

Users can set the background-color of a button through a textbox that accept RGB hexadecimal notation: ff00ff, ccaa22, etc. So I need to set the te

8条回答
  •  甜味超标
    2020-12-08 01:34

    Salaman's code is good but sometimes his inversion is not readable enough. I use YCbCr and just change gray scale.

    function invertColor(rgb) {
        var yuv = rgb2yuv(rgb);
        var factor = 180;
        var threshold = 100;
        yuv.y = clamp(yuv.y + (yuv.y > threshold ? -factor : factor));
        return yuv2rgb(yuv);
    }
    

    jsfiddle demo

提交回复
热议问题