WEEK: 3
Active: January 24th - January 30th
Work Due: January 31st @ 9:00AM

Introduction to Color

Last week, we introduced and discussed the background() function. We have also looked at how you can color shapes using the fill() and stroke() functions. Now that you have had some practice using these, and specifying color using “named color strings”, let’s talk a little more specifically about how you can specify colors in p5.

Specifying Color

As you saw from the background() function documentation page, there are numerous ways to specify color. We have already presented one; “Named Color Strings”. Now let’s look at three more possibilities;

  • Grayscale
  • RGB
  • hex values

Grayscale

Grayscale includes colors that are ‘white’, ‘black’, and any shade of gray in between.

To specify a grayscale color value in p5, the developer needs to provide a single value, as a parameter to any of the color-based functions we have looked at, between 0 (black) and 255 (white). This is for a total of 256 grayscale color values.

In a 0-based system, ‘0’ counts as a value.

256 values is no coincidence. This represents 8 bits (i.e. 8, 1’s or 0’s - 0000 0000).
2 to the power of 8 equals 256. (28 = 256)

Since computers store data in some number of bits, you will often see max numbers, or number ranges, that correspond to some number of 2number of bits.

[ Code Download ] [ View on GitHub ] [ Live Example ]

RGB

RGB and Hex provide developers with a lot more variation and specificity in color selection over “named color strings” or a simple grayscale number. Both of these color systems work by specifying some amount of red, green, and blue that are combined to make the resulting color.

Like grayscale, each color has a possible range of 256 values (0-255).


In RGB, each of these three color values is represented as an integer-based number, comma separated. In p5 we can provide RGB values as a function, wrapped in quotes (i.e. 'rgb(rrr, ggg, bbb)').

For example, in the first code demo, the background is colored orange-ish by using the rgb() function as a input parameter to the background() function.

[ Code Download ] [ View on GitHub ] [ Live Example ]

Each of the color types shown on this page can be used as input parameters to any p5 function that adjusts color in a sketch. i.e.;

  • background()
  • fill()
  • stroke()


Many functions that take color values in p5, will also, more simply, accept three integers as input parameters, representing the RGB value.

The below code achieves the same as above, without the use of the additional 'rgb()' function.

{ TODO: } Replace one of the integer values in the below code example. Remember to use integers between 0-255.

As a reminder, to play with code on this website, simply change the values in the code below and look to the result section to see the difference.

[ Code Download ] [ View on GitHub ] [ Live Example ]

Hexadecimal

Hexadecimal (or hex) values, are written as a single string (characters surrounded by double or single quotes), prepended with a number sign, with 2 characters representing each color. #RRGGBB

THE MAJOR DIFFERENCE is that you can represent 256 values with a two-bit hexadecimal number. In hexadecimal numbering, each digit has 16 values (0-f). And, 16 * 16 = 256.

If we were to translate this to binary;

Binary   Hex
0 = 0
1 = 1
2 = 2
3 = 3
4 = 4
5 = 5
6 = 6
7 = 7
8 = 8
9 = 9
10 = a
11 = b
12 = c
13 = d
14 = e
15 = f


So, in hex, we write the value;

  • 000 as 00
  • 010 as 0a
  • 015 as 0f
  • 016 as 10
  • 017 as 11
  • 031 as 1f
  • 032 as 20
  • 050 as 32
  • 100 as 64
  • 150 as 96
  • 200 as c8
  • 250 as fa
  • 255 as ff


{ TODO: }

For more information on hexadecimal numbering, read;

In p5

To use a hexadecimal color value in p5, we supply it to the function in question, surrounded in quotes. (i.e. #ff00ff)

{ TODO: } Try changing any of the characters after the #. Remember to keep them values between 0-f. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f]

[ Code Download ] [ View on GitHub ] [ Live Example ]

Red, Green, or Blue

So, to specify a color of full red in either system, we set the first number to 255, and the other two to 0.

  • RGB: rgb(255, 0, 0);
  • Hex: #ff0000;


Likewise, to specify a color of full green or full blue, use a value of 255 for the color in question, and 0 for the others.

  • RGB: rgb(0, 255, 0);
  • Hex: ##00ff00;


  • RGB: rgb(0, 0, 255);
  • Hex: ##0000ff;


White & Black

We know from color theory, that white is the presence of all colors and black is the absence of all colors. Knowing this, we can deduce that black would be all 0’s and white would be all full value.

Black:

  • rgb(0, 0, 0);
  • #000000;


White:

  • rgb(255, 255, 255);
  • #ffffff;


Following from this principle, to get any color within the grey scale, we simply need to provide the same value for each color.

#000000 - rgb(0, 0, 0);
#101010 - rgb(16, 16, 16);
#202020 - rgb(32, 32, 32);
#303030 - rgb(48, 48, 48);
#404040 - rgb(64, 64, 64);
#505050 - rgb(80, 80, 80);
#606060 - rgb(96, 96, 96);
#707070 - rgb(112, 112, 112);
#808080 - rgb(128, 128, 128);
#909090 - rgb(144, 144, 144);
#a0a0a0 - rgb(160, 160, 160);
#b0b0b0 - rgb(176, 176, 176);
#c0c0c0 - rgb(192, 192, 192);
#d0d0d0 - rgb(208, 208, 208);
#e0e0e0 - rgb(224, 224, 224);
#f0f0f0 - rgb(240, 240, 240);
#ffffff - rgb(255, 255, 255);

Complementary Colors

When we start to mix the ratios of red, green, and blue, we come up with the rest of the colors of the spectrum. Some of the first colors we should consider are the complementary colors of red, green, and blue. To get the complementary color for red, we use full green and blue. This creates Cyan.

Cyan:

  • rgb(0, 255, 255);
  • #00ffff;
Cyan compliments red.
Cyan compliments red.

Likewise, to get the complimentary colors for green and blue, which are magenta and yellow, respectively, we boost the values of the two other colors.

Magenta:

  • rgb(255, 0, 255);
  • #ff00ff;
Magenta compliments green.
Magenta compliments green.

Yellow:

  • rgb(255, 255, 00);
  • #ffff00;
Yellow compliments Blue.
Yellow compliments Blue.

Other Colors

Others colors are obviously some combination of these values.

For more information on RGB colors, please read the corresponding Wikipedia Article.

Atom Tips:

If you are using atom, there are two packages you may want to consider installing.

  1. Color Picker. The atom color picker lets you inspect colors, as any value type (rgb, hex, hsl, etc.), and navigate a color “picker” so that you can insert any color value. Once, installed, you can pull up this package with the “atom command palette” by typing color. Atom Color Picker Demo

  2. Pigments. The atom pigments package highlights color values in your code. This can make it easier to visually see in atom, while developing the colors that will appear. Atom Pigments Package Demo

{ TODO: }


Previous section: