p5.js | setGreen() Function

The setGreen() function in p5.js is used to set the green color value in RGB color mode. It sets the second value of RGB format.

Syntax:

setGreen(green)

Parameters: The function accepts single parameter as mentioned above and described below:



  • green: This parameter stores the new green value.

Below programs illustrate the setGreen() function in p5.js:
Example-1: This example uses setGreen() function to set red value of RGB color format.

filter_none

edit
close

play_arrow

link
brightness_4
code

/* declare a variable
to store the value of color*/
let backgroundColor;
  
function setup() {
    /* initialise the variable
    with RGB color format*/
    backgroundColor =
      color(13, 0, 150);
}
  
function draw() {
    /* Create Canvas of a given size*/
  
    createCanvas(500, 500);
  
    // Use of setGreen function
    backgroundColor.setGreen(
      0 + 128 * cos(millis() / 1000));
  
    /* Pass the initialised variable 
      to background function*/
    background(backgroundColor);
  
    // Set text size
    textSize(30);
  
    // Set text color
    fill("white");
  
    // set text
    text("GeeksForGeeks", 125, 125);
  
}

chevron_right


Output:

Example-2: This example uses setGreen() function to set red value of RGB color format.

filter_none

edit
close

play_arrow

link
brightness_4
code

/* declare a variable to 
store the value of color.*/
let backgroundColor;
  
function setup() {
    /* initialise the variable
    with RGB color format.*/
    backgroundColor = 
      color(0, 0, 0);
}
  
function draw() {
    // Create Canvas of a given size
  
    createCanvas(540, 500);
  
    /* Use of setGreen function
    and initialise it to maximum.*/
    backgroundColor.setGreen(255);
  
    /* Pass the initialised variable
    to background function.*/
    background(backgroundColor);
  
    // Set text size
    textSize(30);
  
    // Set text color
    fill("green");
  
    // set text
    text("GeeksForGeeks\n ", 135, 125);
    text(
      "A Computer Science Portal For Geeks"
      , 10, 155);
  
}

chevron_right


Output:

Reference:https://p5js.org/reference/#/p5.Color/setGreen



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.