RGB color with ranges of 0 to 255 is not the only way you can handle color in p5.js, in fact, it allows us to think about color any way we like. (If you are interested in programming "rose-colored" glasses, this is where you would begin.)Īlpha values also range from 0 to 255, with 0 being completely transparent (i.e., 0% opaque) and 255 completely opaque (i.e., 100% opaque). Behind the scenes, p5.js takes the color numbers and adds a percentage of one to a percentage of another, creating the optical perception of blending.
It is important to realize that pixels are not literally transparent, this is simply a convenient illusion that is accomplished by blending colors. The alpha values for an image are sometimes referred to collectively as the "alpha channel" of an image. Alpha means transparency and is particularly useful when you want to draw elements that appear partially see-through on top of one another. In addition to the red, green, and blue components of each color, there is an additional optional fourth component, referred to as the color's "alpha".
You will get the hang of RGB color mixing through experimentation, but next we will cover some code using some common colors. As with grayscale, the individual color elements are expressed as ranges from 0 (none of that color) to 255 (as much as possible), and they are listed in the order R, G, and B. And of course you can't say "Mix some red with a bit of blue," you have to provide an exact amount. While this may take some getting used to, the more you program and experiment with RGB color, the more it will become instinctive, much like swirling colors with your fingers. This assumes that the colors are all as bright as possible, but of course, you have a range of color available, so some red plus some green plus some blue equals gray, and a bit of red plus a bit of blue equals dark purple. And with color on the screen, you are mixing light, not paint, so the mixing rules are different as well. First, the primaries are different: red, green, and blue (i.e., "RGB" color). Digital colors are also constructed by mixing three primary colors, but it works differently from paint. The more paint you added, the darker it got. Swirling all colors together resulted in a muddy brown. Remember finger painting? By mixing three "primary" colors, any color could be generated. In addition, if we draw two shapes, p5.js will always use the most recently specified stroke and fill, reading the code from top to bottom. Also, remember not to eliminate both-with noStroke() and noFill(), nothing will appear! Our instinct might be to say "stroke(0)" for no outline, however, it is important to remember that 0 is not "nothing", but rather denotes the color black. Stroke or fill can be eliminated with the functions: noStroke() and noFill(). Stroke(0) // Setting the outline (stroke) to blackįill(150) // Setting the interior of a shape (fill) to grey Here's an example.īackground(255) // Setting the background to white
There is also the function background(), which sets a background color for the window. In between, every other number-50, 87, 162, 209, and so on-is a shade of gray ranging from black to white.īy adding the stroke() and fill() functions before something is drawn, we can set the color of any given shape. Let's start with the simplest case: black & white or grayscale. Color, rather, is defined as a range of numbers.
Saying "Hey, can you make that circle bluish-green?" will not do. In the digital world, when we want to talk about a color, precision is required. If you find any errors or have comments, please let us know. This tutorial is from the book Learning Processing by Daniel Shiffman, published by Morgan Kaufmann, © 2008 Elsevier Inc.