The green() function in p5.js is used to extract the green value from a color or pixel array.
Syntax:
green(c)
Parameters: This function accepts single parameter c which stores the p5.Color object, color components, or CSS color.
Below programs illustrate the green() function in p5.js:
Example 1: This example uses green() function to extract the green value from a color or pixel array.
function setup() {
createCanvas(300, 80);
}
function draw() {
background(220);
let c = color(0, 126, 255, 102);
let y = green(c);
textSize(16);
fill(color( 'red' ));
text( "Green Value is : " + y, 50, 30);
}
|
Output:

Example 2: This example uses green() function to extract green value and fill it to the rectangle.
function setup() {
createCanvas(160, 180);
}
function draw() {
background(220);
let c = color(0, 126, 100, 34);
let value = green(c);
fill(0, value, 0);
rect(50, 15, 35, 70);
text( "Value of green is : " + value, 22, 110);
}
|
Output:

Reference: https://p5js.org/reference/#/p5/green