Ash*_*ley 41 javascript rgb coding-style function colors
我试图改变下面的功能swapFE()的颜色,我无法弄清楚如何写它.我被告知要将短语节点的颜色更改为颜色值(155,102,102).我试着这样做,你可以在函数的末尾看到see- parent.childNodes [1] .style.color =(155,102,102); 但它只是一个深蓝色的海军蓝.它应该是一个棕红色.我不知道我做错了什么.如何解决此问题以获得正确的RGB颜色?我知道我有其余的权利,它只是想弄清楚如何写出给我带来问题的颜色和价值.谢谢!
//this function changes the French phrase to an English phrase.
function swapFE(e) {
var phrase = e.srcElement;
//phrase.innerText = english[phrase.id];
var parent = phrase.parentNode;
//childNodes[0] is the number of the phrase +1
var idnum = parent.childNodes[0];
//parseInt takes a textstring and extracts it to make a number. Then you will subtract 1 from the number.
var phrasenum = parseInt(idnum.innerHTML)-1;
phrase.innerText = english[phrasenum];
parent.childNodes[1].style.fontStyle= "normal";
parent.childNodes[1].style.color= (155, 102, 102);
}
function swapEF(e) {
var phrase = e.srcElement;
//phrase.innerText = english[phrase.id];
var parent = phrase.parentNode;
var idnum = parent.childNodes[0];
var phrasenum = parseInt(idnum.innerHTML)-1;
phrase.innerText = french[phrasenum];
parent.childNodes[1].style.fontStyle= "italic";
parent.childNodes[1].style.color= "black";
Run Code Online (Sandbox Code Playgroud)
Mar*_*ius 63
尝试:
parent.childNodes[1].style.color = "rgb(155, 102, 102)";
Run Code Online (Sandbox Code Playgroud)
要么
parent.childNodes[1].style.color = "#"+(155).toString(16)+(102).toString(16)+(102).toString(16);
Run Code Online (Sandbox Code Playgroud)
cur*_*ran 15
这是一个简单的函数,可以从0到255的RGB值创建一个CSS颜色字符串:
function rgb(r, g, b){
return "rgb("+r+","+g+","+b+")";
}
Run Code Online (Sandbox Code Playgroud)
或者(创建更少的字符串对象),您可以使用数组join():
function rgb(r, g, b){
return ["rgb(",r,",",g,",",b,")"].join("");
}
Run Code Online (Sandbox Code Playgroud)
如果(r,g和b)是0到255之间的整数,上述函数将只能正常工作.如果它们不是整数,颜色系统会将它们视为0到1的范围.要考虑非整数数字,使用以下内容:
function rgb(r, g, b){
r = Math.floor(r);
g = Math.floor(g);
b = Math.floor(b);
return ["rgb(",r,",",g,",",b,")"].join("");
}
Run Code Online (Sandbox Code Playgroud)
小智 9
这是更好的功能
function RGB2HTML(red, green, blue)
{
var decColor =0x1000000+ blue + 0x100 * green + 0x10000 *red ;
return '#'+decColor.toString(16).substr(1);
}
Run Code Online (Sandbox Code Playgroud)
我展示了一个添加随机颜色的例子。你可以这样写
var r = Math.floor(Math.random() * 255);
var g = Math.floor(Math.random() * 255);
var b = Math.floor(Math.random() * 255);
var col = "rgb(" + r + "," + g + "," + b + ")";
parent.childNodes[1].style.color = col;
Run Code Online (Sandbox Code Playgroud)
该属性应为字符串