Open In App

p5.js setRed() Function

Last Updated : 07 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The setRed() function in p5.js is used to set the red color value in RGB color mode. It sets the first value of RGB format.

Syntax:

setRed(red)

Parameters:

  • red: This parameter stores the new red value.

Example 1: This example uses setRed() function to set red value of RGB color format.

javascript
/* declare a variable to 
 store the value of color*/
let backgroundColor;

function setup() {
    /* initialise the variable
     with RGB color format*/
    backgroundColor = color(0, 50, 150);
}

function draw() {
    // Create Canvas of a given size

    createCanvas(500, 500);

    // Use of setRed function
    backgroundColor.setRed(
      0 + 128 * cos(millis() / 1000));

    /* Pass the initialized 
      variable to background function*/
    background(backgroundColor);

    // Set text size
    textSize(30);

    // Set text color
    fill("white");

    // set text
    text("GeeksForGeeks", 125, 125);

}

Output:

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

javascript
/* 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 setRed function 
    and initialise it to maximum*/
    backgroundColor.setRed(255);

    /* Pass the initialised
    variable to background function*/
    background(backgroundColor);

    // Set text size
    textSize(30);

    // Set text color
    fill("white");

    // set text
    text("GeeksForGeeks\n ", 135, 125);
    text(
      "A Computer Science Portal For Geeks",
      10, 155);

}

Output:



Next Article

Similar Reads