Open In App

p5.js setBlue() Function

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

The setBlue() function in p5.js is used to set the blue color value in RGB color mode. It sets the third value of the RGB format.

Syntax:

setBlue(blue)

Parameters:

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

  • blue: This parameter stores the new blue value.

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

JavaScript
// Declare a variable to store the value of color
let backgroundColor;

function setup() {
  // Create Canvas of a given size (should be in setup to run once)
  createCanvas(500, 500);

  // Initialise the variable with RGB color format
  backgroundColor = color(13, 40, 0);
}

function draw() {
  // Use of setBlue function to change the blue channel dynamically
  backgroundColor.setBlue(0 + 128 * cos(millis() / 1000));

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

  // Set text size
  textSize(30);

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

  // Display the text
  text("GeeksForGeeks", 125, 125);
}

Output:

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

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

function setup() {
  // Create Canvas of a given size 
  // (should be done in setup to run once)
  createCanvas(540, 500);

  // Initialise the variable with RGB color format
  backgroundColor = color(0, 0, 0);
}

function draw() {
  // Set the blue channel to its maximum value
  backgroundColor.setBlue(255);

  // Use the background color for the canvas background
  background(backgroundColor);

  // Set text size
  textSize(30);

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

  // Display the text with line breaks
  text("GeeksForGeeks\nA Computer Science Portal For Geeks", 10, 125);
}

Output:


Next Article

Similar Reads