颜色计算 [英] Color computation
问题描述
如果我有:
< div style =" background:anyColorHere" Hi< / div>
其中anyColorHere是表示有效颜色的任何十六进制字符串。
如何反转背景颜色anyColorHere ?你能指出
(或建议)一个脚本
以正确的方式做到这一点吗?
-P >
推荐答案
pa ** *********@libero.it 写道:
pa***********@libero.it wrote:
< div style =" background:anyColorHere" Hi< / div>
其中anyColorHere是表示有效颜色的任何十六进制字符串。
如何将背景颜色反转为anyColorHere ?你能指出
(或建议)一个以正确方式做到这一点的脚本吗?
<div style="background:anyColorHere"Hi </div>
where anyColorHere is any hex string that represent a valid color.
How can I invert that background color "anyColorHere" ? Can you point
(or suggest) a script that does that in a proper way ?
< html>
< head>
< script type =" text / JavaScript" ;>
函数bla(){
var b = document.getElementsByTagName(''body'')[0];
var bg = b.style.backgroundColor;
alert(''现在的背景是''+ b.style.backgroundColor)
alert(''现在的背景将变成反转的RGB颜色'')
var oldCol = bg.split(''('')[1] .split('')'')[0] .split('','' );
var newCol = new Array()
for(var i = 0; i< oldCol.length; i ++){
newCol [i] = 255-Number(oldCol [i]);
}
b.style.backgroundColor
=''rgb(' '+ newCol [0] +'',''+ newCol [1] +'',''+ newCol [2] +'')'';
alert(''新背景是''+ b.style.backgroundColor)
}
onload = bla
< / script>
< / head>
< body style =&q uot; background-color:rgb(0,204,255)">
< / body>
< / html>
Soli Deo Gloria:
http: //www.codingforums.com/archive/...p?t-47335.html
Hex / RGB转换器:
http://www.google.com/search?q=conve .. .rgb + javascript
-
巴特
<html>
<head>
<script type="text/JavaScript">
function bla(){
var b = document.getElementsByTagName(''body'')[0];
var bg = b.style.backgroundColor;
alert(''present background is ''+b.style.backgroundColor)
alert(''present background will be change into inversed RGB color'')
var oldCol = bg.split(''('')[1].split('')'')[0].split('','');
var newCol = new Array()
for(var i=0;i<oldCol.length;i++){
newCol[i] = 255-Number(oldCol[i]);
}
b.style.backgroundColor
= ''rgb(''+newCol[0]+'',''+newCol[1]+'',''+newCol[2]+'')'';
alert(''new background is ''+b.style.backgroundColor)
}
onload=bla
</script>
</head>
<body style="background-color: rgb(0,204,255)">
</body>
</html>
Soli Deo Gloria:
http://www.codingforums.com/archive/...p?t-47335.html
Hex/RGB converter:
http://www.google.com/search?q=conve...rgb+javascript
--
Bart
写于2006年9月9日的comp.lang.javascript :
wrote on 09 sep 2006 in comp.lang.javascript:
如果我有:
< div style =" background:anyColorHere" Hi< / div>
其中anyColorHere是代表有效颜色的任何十六进制字符串。
如何将背景颜色反转为anyColorHere? ?你能指出
(或建议)一个以正确方式做到这一点的脚本吗?
If I have:
<div style="background:anyColorHere"Hi </div>
where anyColorHere is any hex string that represent a valid color.
How can I invert that background color "anyColorHere" ? Can you point
(or suggest) a script that does that in a proper way ?
试试:
< script type =''text / javascript''>
function invertMe(x){
var t1 =''0123456789abcdef#''
var t2 =''fedcba9876543210#''
x.style.backgroundColor =
x.style.backgroundColor.replace(/./gi,
function(s){
return t2 .charAt(t1.indexOf(s));
}
)
}
< / script> ;
< div style =''background-color:#c0c0c0; width:50px;''
onclick =''invertMe(this)' '>
点击我以反转我的背景< / div>
< br>< br>
< div style = ''background-color:#fe3456; width:50px;''
onclick =''invertMe(this)''>
点击我反转我的背景< / div>
-
Evertjan。
荷兰。
(请更改x''es到我的电子邮件地点的点数)
try:
<script type=''text/javascript''>
function invertMe(x){
var t1 = ''0123456789abcdef#''
var t2 = ''fedcba9876543210#''
x.style.backgroundColor =
x.style.backgroundColor.replace( /./gi,
function (s) {
return t2.charAt(t1.indexOf(s));
}
)
}
</script>
<div style=''background-color:#c0c0c0;width:50px;''
onclick=''invertMe(this)''>
Click me to invert my background</div>
<br><br>
<div style=''background-color:#fe3456;width:50px;''
onclick=''invertMe(this)''>
Click me to invert my background</div>
--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)
x.style.backgroundColor.replace(/./gi,
x.style.backgroundColor.replace( /./gi,
function(s){
返回t2.charAt(t1.indexOf(s));
function (s) {
return t2.charAt(t1.indexOf(s));
....
我在这里向你重播,Bart和Evertjan。
>
这两个解决方案都运行正常。
据我所知,巴特依赖于
颜色定义为rgb( XX,YY,ZZ)" ;.实际上我在我的
问题中假设一个十六进制字符串。但确实有这种方法也很好。
Evertjan提出的脚本是对我的问题的预先答复
我觉得它实现的逻辑很聪明它还具有
的优势,可以屏蔽许多复杂的可能
解析。
我非常喜欢它。
btw这是什么:< div style =''background-color:#c0c0c0; width:50px;''
onclick =''invertMe(this)' '点击我反转我的背景< / div>
在IE中它什么也没显示。这个代码是否可以测试其他一些
浏览器?
-P
....
I replay here to you both, Bart and Evertjan.
Both solution works fine.
Bart is relying, as far as I understand, on the fact that
color is defined as rgb(xx,yy,zz)". Actually I assumed in my
question an hex string. But it is nice indeed to have this method also.
The script proposed by Evertjan is a precide reply to my question
and I feel the logic it implements is quite smart. It has also
the advantage to shield from then many complexities of a possible
parsing.
I like it a lot.
btw What is this : <div style=''background-color:#c0c0c0;width:50px;''
onclick=''invertMe(this)''Click me to invert my background</div>
in IE it does displays nothing. Is this code to test some other
browser?
-P
这篇关于颜色计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!