views:

48

answers:

1

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 be 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.

+2  A: 

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));
Sergey Ilinsky
@Segey: Nice Solution, thx
Shantanu Gupta
@Shantanu Gupta You are welcome
Sergey Ilinsky