Stroke & Fill
In p5, color can be applied to shapes primarily using two functions: fill
and stroke
.
Fill applies color to the inside of the shape. Stroke is applied as the color of the outline of the shape.
This is similar to how shapes are created in Adobe Illustrator.
Let's add some color to a simple self-portrait.
HTML Named Values
p5 is built on JavaScript, a web based programming language, used to interact with HTML and CSS. Because of this, and the way the HTML Canvas API works, p5 can use HTML Named Colors as color values.
Any color that works in HTML can be used in p5, but in p5 the syntax is slightly different.
A CSS color rule:
body { color: blue; }
A p5 color:
fill("blue");
In JavaScript, the HTML color name is treated like a string
, which is a string of characters bound by quotation marks.
noStroke & noFill
In some cases you may not want to see a stroke or fill on your shapes. The stroke can be removed with the noStroke
function, and likewise the fill is removed with noFill
.
Both can be brought back later in the program just by setting the stroke
or fill
to a new color.
strokeWeight
The stroke of the mouth is pretty hard to see now. The thickness of the stroke is 1 pixel by default, but that can be changed with the strokeWeight
function.
HTML HEX values
Like the HTML color names p5 also uses HEX values like #ffddff
as color values.
These are also strings, so they need to be formatted with quotation marks "#ffddff"
Grayscale values
p5 also uses numerical values as arguments to represent color in several modes.
One numerical value in the range from 0
to 255
will create a grayscale color, with 0
representing black, 255
white, and everything else a shade of gray.
The background
function in our examples uses the value 220
which is a light gray. Try changing it to 0
or 255
or something in between.
RGB Values
Using numerical values, the color functions can mix levels of red, green and blue together to create all of the possible colors for the computer screen.
The order is always red, green, blue.
This is useful for doing programmatic things with color, which we will explore in later classes.
Transparency
Adding a fourth argument to RGB will control the transparency or alpha level of the color.
This makes colors that show through to what is behind them.