Submodule 16.3: Colors
Site: | ΕΛ/ΛΑΚ Moodle |
Course: | Study / Web Design and Web Development |
Book: | Submodule 16.3: Colors |
Printed by: | Guest user |
Date: | Sunday, 20 April 2025, 3:55 AM |
Description
- Color functions
- Color rules
- Alpha value - Transparency
Color functions
In order to apply color to p5.js elements we use the fill ()
function.
We can write colors in any of RGB, RGBA, Hex and supported named color strings.
In order to define the color of the border of p5.js elements we use the stroke()
function.
We can increase or decrease the border weight by using the strokeWeight()
function.
If we don't want our elements to be filled, we use the noFill()
function, whereas if we don't want them to have border we use the noStroke()
Example
Here is an example where all of the above functions are used:
function setup() {
// create canvas
createCanvas(800,500);
}
function draw () {
// set background color
background('#B1CBBB');
//use the fill function to color the triangle
fill('#EEA29A');
// use the noStroke function to remove border
noStroke();
triangle(width/2,height/2,500,400,300,400);
// use the noFill function to remove the fill of our element
noFill();
// use the stroke to change the color of the border
stroke('#D64161');
// use the strokeWeight to change the border weight
strokeWeight(5);
triangle(width/2,height/2,500,100,300,100);
}
- See more about the fill() function
- See more about the noFill() function
- See more about the stroke() function
- See more about the noStroke() function
- See more about the strokeWeight() function
Color rules
In order to define the color of different p5.js elements it is important to understand the color rules.
When we want to color an element, we have to apply the appropriate functions prior to drawing the element.
Example:
function setup() {
// create canvas
createCanvas(800,500);
// set background color
background('orange');
}
function draw () {
fill('red');
stroke('#f12e78');
ellipse(width/2,height/2, 50);
fill('#DC143C');
stroke('#FFA500');
ellipse(500,height/2, 50);
}
If we don't define any different color all the elements that will be drawn after this, they will have the same colors.
Example:
function setup() {
// create canvas
createCanvas(800,500);
// set background color
background('orange');
}
function draw () {
fill('red');
stroke('#f12e78');
ellipse(width/2,height/2, 50);
ellipse(width/2,height/3, 50);
rect(width/2,height/6, 50,50);
ellipse(width/4,height/2, 50);
}
If we want each color function to be applied only on specific elements, we can position our elements in between the push()
and pop()
functions.
For example the above can be modified as :
function setup() {
// create canvas
createCanvas(800,500);
// set background color
background('orange');
}
function draw () {
push();
fill('red');
stroke('#f12e78');
ellipse(width/2,height/2, 50);
pop();
ellipse(width/2,height/3, 50);
rect(width/2,height/6, 50,50);
ellipse(width/4,height/2, 50);
}
Exercise
- Open your Visual Studio editor and the
p5yourName
folder. - Open the file
ex812.js
in your editor and save it asex832.js
- Open the file
ex812.html
in your editor and save it asex832.html
- In the
ex832.html
file, update the link toex832.js
from exersice812.js - Go to the
index.html
file and create, underModule 8
, alink
to theex832.html
file with the title "Color rules".
Modify the ex832.js
file to apply colors to the face. You can see here an example.
function setup() {
createCanvas(800, 600);
// set background color
background('#D66761');
}
function draw() {
// state the fill color of the shape
fill('#FBEFCC');
// state the border color of the shape
stroke('#FBEFCC');
// state the border weight of the shape
strokeWeight(2);
// create the face ellipse
ellipse(width/2,height/2,200,300)
// state the border color of the outer eye circle
stroke('#A2836E');
// for the left eye
// draw the base ellipse, the sclera/“white of the eye”
ellipse(350,275,50,25);
// state the border of the iris
stroke('black');
// draw the Iris for the eye
ellipse(350,275,25);
// fill the center of the eye
fill('#034F84');
// color the border
stroke('#034F84');
// draw the center of the eye
ellipse(350,275,12.5);
// state the border color of the outer eye circle
stroke('#A2836E');
// for the right eye
// draw the base ellipse for the eye, the sclera/“white of the eye”
noFill();
ellipse(450,275,50,25);
// state the border of the iris
stroke('black');
// draw the Iris for the eye
ellipse(450,275,25);
// fill the center of the eye
fill('#034F84');
// color the border
stroke('#034F84');
// draw the center of the eye
ellipse(450,275,12.5);
}
Do a Git commit with the message "Color rules".
Alpha value - Transparency
When we want to determine the alpha value of a p5.js element, we define a fourth value for the color.
The smaller the alpha value the more transparent the element.
Example
Here is an example code of two ellipses with different alpha values :
function setup() {
// create canvas
createCanvas(800,500);
// set background color
background('#B2B2B2');
}
function draw () {
//this ellipse will have a low transparency value
fill (175,68,39,50);
ellipse(width/2,height/2,100);
// this ellipse will have a high transparency value
fill (124,156,39,5);
ellipse(450,height/2,100);
}
Exercise
- Open your Visual Studio editor and the
p5yourName
folder. - Open the file
ex812.js
in your editor and save it asex833.js
- Open the file
ex812.html
in your editor and save it asex833.html
- In the
ex833.html
file, update the link toex833.js
from exersice812.js - Go to the
index.html
file and create, underModule 8
, alink
to theex833.html
file with the title "Alpha value".
Modify the ex833.js
file to create an ellipse which will move horizontally from the left to the right end of the canvas and will change its color and alpha value according to the direction it has. You can see here an example.
// create a variable tp control the direction of the ellipse
let control = false;
// make ellipse starting from the left end of the canvas
let i=30;
function setup() {
// create canvas
createCanvas(800,500);
// set background color
background('#31bc33');
}
function draw () {
// when ellipse is in the left edge, control would be/become false
if (i==30) {
control = false;
}
// if ellipse is inside the canvas width (we substract 30 which is the radius
// of our ellipse) and control is false
if (i<(width-30) && control==false){
// state fill and alpha of the ellipse
fill(146,87,67,40);
//state stroke color and transparency
stroke(234,65,88,5);
i++
// increase its x position (ellipse moves in the right direction)
ellipse(i,height/2,60);
}
else {
// else control will change to true
control = true;
//change the color and fill of the ellipse
// state fill and alpha of the ellipse
fill(56,57,67,100);
//state stroke color and transparency
stroke(5,165,188,50);
// decrease its x position (ellipse moves in the left direction)
i--;
ellipse(i,height/2,60);
}
}
Do a Git commit with the message "Alpha value".
- See more about the alpha() function