Representing Colors

Each pixel on your computer monitor is a composite of three colors: red, green, and blue. If you take those three colors and mix them in different quantities, you get just about every other color you can imagine, from red, yellow, and green, to cyan, blue, and purple, and everything in between.

If you let red, green, and blue each be a value between 0 and 1 (with 0 meaning the color is entirely absent, and 1 meaning the color is fully present), then the figure shows some possible colors you can get by combining them.

images/canvas/color-wheel.png

If all components are 1, you get white. If all components are 0, you get black.

And did you catch that? A color is a tuple, just like vectors and points! In fact, when it comes time to make this real, it may make sense to build your color implementation on top of your tuple implementation, rather than starting from scratch.

One way or another, you’re going to need to be able to create a color from a (red, green, blue) tuple. Add the following test, which does just that.

 Scenario​: Colors are (red, green, blue) tuples
 Given​ c ← color(-0.5, 0.4, 1.7)
 Then​ c.red = -0.5
 And​ c.green = 0.4
 And​ c.blue = 1.7

In practice, you’ll only use numbers between 0 and 1 for those components, but don’t put any constraints on them just yet. If a color is especially bright or dark somewhere in your scene, it may go through multiple transformations before reaching your virtual “eye,” dropping it to less than 0 or increasing it to greater than 1 at any point along the way. Limiting the color prematurely can make parts of your scene too bright or dark in the final image.

Once that test is passing, move on. We’ll talk about the different operations that your color implementation will need to support.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.171.20