Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops The Refactor Challenge

Quinton Dobbs
Quinton Dobbs
5,149 Points

How does javascript know to assign colors to red, green, and blue?

My code works. I just don't entirely understand what is happening. Is "rgb" a keyword that knows to change number values of red, green, and blue to levels of pigment (I don't know what to call it)? In other words, how does JavaScript know that (red = 256) equates to the color red while (apple = 256) doesn't?

Quinton Dobbs
Quinton Dobbs
5,149 Points

I think I found my answer on the next video. Red, blue, and green are keywords for their respective colors, right?

1 Answer

Steven Parker
Steven Parker
229,732 Points

The names don't matter, it's how they are used.

But those names are sensible choices for what they represent.

The names red, green, and blue are just variables in your program. They could be any names. But they are given values in the range 0-255 which is valid for a color value. Then these values are combined into a string that contains the rgb function name and they become the 3 arguments for that function. For examples: pure green would be "rgb(0,255,0)" and hot pink would be "rgb(255,0,153)".

Then this complete string becomes the value of the background-color style property of an HTML element, and that's what causes the actual color to appear.

JavaScript doesn't know these are colors, but the style engine in the browser does.