wrote on 09 sep 2006 in comp.lang.javascript:
MSIE shows "#c0c0c0"
FF shows "rgb(192,192,192)"
Damn!
Right.
I just tried it: with Firefox your nice function seems to quit working.
After all if Firefox converts always to rgb() one could argue that this
could make more "omogeneous" the programming. The problem is that
usually we want these things to work with all major browsers...
So is the best approach to convert all to rgb() or should we test each
time if is an hex representation or a rgb ?
I hate "best" approaches.
<script type='text/javascript'>
function invertMe(x){
var t1 = '0123456789abcdef#'
var t2 = 'fedcba9876543210#'
var c = x.style.backgroundColor
// begin FF patch
if (/rgb/.test(c)) {
c = c.replace(/rgb\(|\)/g,'').split(',')
c = '#'+thex(c[0])+thex(c[1])+thex(c[2])
}
// end FF patch
x.style.backgroundColor =
c.replace( /./gi,
function (s) {
return t2.charAt(t1.indexOf(s));
}
)
}
function thex(x) {
x = +x
return (x>15)?x.toString(16)
'0'+x.toString(16))
}
</script>
<div style='background-color:#c0c0c0;width:250px;'
onclick='invertMe(this)'>
Click me to invert my background</div>
<br><br>
<div style='background-color:#fe3456;width:250px;'
onclick='invertMe(this)'>
Click me to invert my background</div>