开发者

How to invert the forecolor and backcolor when any one of them changes

I am using a ajaxtoolkit to choose a color. I want to set the forecolor of the text as the reverse of backcolor.

if a black has been choosen as backcolor then its corresponding forecolor should b开发者_开发知识库e set to white and vice versa.

I want to do this with javascript

<script type="text/javascript">
    function colorChanged(sender) {
//        sender.get_element().style.color =
//       "#" + sender.get_selectedColor();
        sender.get_element().value="#" + sender.get_selectedColor();

    }   
</script>

Back color is being set by ajax javascript. What color has been set can be obtained above by using sender.get_selectedColor();. How to reverse this color. I think it can be possible using regex but dont know how to do it.


You need to get every color compound from RRGGBB, revert it and then glue back:

function numberToHex(nValue, nLength/* =2 */) {
    var sValue  = Math.abs(Math.floor(nValue)).toString(16);
    if (!nLength)
        nLength = 2;
    if (sValue.length < nLength)
        sValue  = Array(nLength + 1 - sValue.length).join('0') + sValue;
    return sValue;
};

inverted = numberToHex(255 - parseInt(original.substr(0, 2), 16)) 
           + numberToHex(255 - parseInt(original.substr(2, 2), 16)) 
           + numberToHex(255 - parseInt(original.substr(4, 2), 16));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜