Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a RGB color value in JavaScript?

I am trying to change the color of the function swapFE() below and I can't figure out how to write it. I was told to change the color of the phrase node to the color value (155, 102, 102). I tried to do that as you can see at the end of the function see- parent.childNodes[1].style.color= (155, 102, 102); but it just comes out a dark navy blue. It's supposed to be a brownish red. I have no idea what I'm doing wrong. How can I fix this to get the correct RGB color? I know I have the rest right it's just figuring out how to write the color and the value that's giving me problems. Thanks!

//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";  
like image 300
Ashley Avatar asked Jan 31 '10 20:01

Ashley


People also ask

How define RGB in JavaScript?

CSS rgb() Function The rgb() function define colors using the Red-green-blue (RGB) model. An RGB color value is specified with: rgb(red, green, blue). Each parameter defines the intensity of that color and can be an integer between 0 and 255 or a percentage value (from 0% to 100%).

How do you write RGB values?

RGB Color Values For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest value (255), and the other two (green and blue) are set to 0. Another example, rgb(0, 255, 0) is displayed as green, because green is set to its highest value (255), and the other two (red and blue) are set to 0.

What is the correct syntax for RGB color?

RGB color model RGB colors can be expressed through both hexadecimal (prefixed with # ) and functional ( rgb() , rgba() ) notations. R (red), G (green), B (blue), and A (alpha) are hexadecimal characters (0–9, A–F). A is optional. For example, #ff0000 is equivalent to #ff0000ff .


1 Answers

try:

parent.childNodes[1].style.color = "rgb(155, 102, 102)";  

Or

parent.childNodes[1].style.color = "#"+(155).toString(16)+(102).toString(16)+(102).toString(16); 
like image 166
Marius Avatar answered Sep 22 '22 05:09

Marius