p5.js | lightness() function

The lightness() function in p5.js is used to extract the HSL lightness value from a color or pixel array.

Syntax:

lightness(c)

Parameters: This function accepts single parameter c which stores the p5.Color object, color components, or CSS color.



Below programs illustrate the lightness() function in p5.js:

Example 1: This example uses lightness() function to extract the HSL lightness value from pixel array.

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
      
    // Create Canvas of size 300*80
    createCanvas(300, 80);
}
  
function draw() {
      
    // Set background color
    background(220);
      
    // Initialize color mode to HSB
    colorMode(HSL, 255);
      
    // Initialize the parameter
    let c = color(0, 126, 100);
      
    // Sets 'value' to 100
    let value = lightness(c); 
  
    // Set the font size
    textSize(16);
  
    // Set the font color
    fill(color('red'));
      
    // Display result
    text("Lightness Value is : " + value, 50, 30);
}

chevron_right


Output:

Example 2: This example uses lightness() function to extract the HSL lightness value from pixel array and use it as grayscale value.

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
      
    // Create Canvas of size 300*80
    createCanvas(300, 180);
}
  
function draw() {
      
    // SEt background color
    background(220);
      
    // Initialize color mode to HSB
    colorMode(HSL, 255);
      
    // Initialize the parameter
    let c = color(56, 126, 10);
      
    // Sets 'value' to 10
    let value = lightness(c); 
      
    // Fill the color value
    fill(value);
      
    // Create rectangle
    rect(50, 15, 35, 70);
      
    // Display result
    text("Value of Lightness is : " + value, 22, 110);
}

chevron_right


Output:

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



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.



Improved By : ManasChhabra2



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.