Skip to content
Skip to main content

About this free course

Download this course

Share this free course

Algorithmic Design
Algorithmic Design

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

3.4 Principle 4: Colour

Knowing how to set colours is an important part of creative coding and will be crucial for your wallpaper project. On this page, you will learn how to use the fill and stroke commands to use the full colour spectrum.

By default, p5.js defines colour values using RGB (Red, Green, and Blue) as a value ranging from 0 to 255, where 0 signifies the absence of the colour and 255 represents the highest intensity of that colour. These three colours are the primary colours, from which additional colours can be created by blending these colours in varying ratios. For example, a yellow colour in RGB is created using a mix of red and green using the RGB values: 255,255,0.

Described image
Figure  8  How RGB colours mix; the maximum value of all three primary RGB colours creates a white colour: 255,255,255

In Program 4 below, click on the small rectangular box on the canvas and you should see a colour picker appear. It allows you to pick different colours and see their RGB values at the same time.

Use the colour picker to change the colour of the square a few times. Notice how the numerical RGB values change as you select different colours.

Active content not displayed. This content requires JavaScript to be enabled.
Program 4  RGB colour picker
Interactive feature not available in single page view (see it in standard view).

Filled colour

The fill() command allows you to set the fill colour inside shapes. You are defining three values because you are mixing the three RGB colours together to create a single fill colour fill(R, G, B):

  • R = red value between 0 and 255

  • G = green value between 0 and 255

  • B = blue value between 0 and 255.

(p5.js reference: fill [Tip: hold Ctrl and click a link to open it in a new tab. (Hide tip)] )

Outline colour

The stroke() command controls the outline colour of any shape, for example stroke(R, G, B) (p5.js reference: stroke).

Note that you can also use the noStroke() command to disable any stroke being drawn (p5.js reference: noStroke).

Active content not displayed. This content requires JavaScript to be enabled.
Program 5  Recolour the squares
Interactive feature not available in single page view (see it in standard view).
Download this video clip.Video player: Video 6
Copy this transcript to the clipboard
Print this transcript
Show transcript | Hide transcript
Video 6  Changing and playing with colour
Interactive feature not available in single page view (see it in standard view).

Activity 4  Recolour the squares

Timing: Allow 20 minutes to complete this activity

Use Program 5 Recolour the squares to tweak the numerical RGB colour values for the fill and outline of the squares.

Experiment with inserting mouseX or mouseY in place of the numerical RGB values as well as parameters of the squares, as shown in Video 6.