Open In App

p5.js saturation() function

Last Updated : 11 Aug, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report
The saturation() function in p5.js is used to extract the HSL and HSB saturation value from a color or pixel array. Syntax:
saturation(c)
Parameters: ThIS function accepts single parameter c which stores the p5.Color object, color components, or CSS color. Below programs illustrate the saturation() function in p5.js: Example 1: This example uses saturation() function to extract the HSL and HSB saturation value from the pixel array. javascript
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 126
    let value = saturation(c); 
    
    // Set the font size
    textSize(16);
    
    // Set the font color
    fill(color('red'));
    
    // Display result
    text("Saturation Value is : "
            + int(value), 50, 30);
}
Output: Example 2: This example uses saturation() function to extract the HSL and HSB saturation value from the pixel array and fill this as grayscale value. javascript
function setup() {

    // Create Canvas of size 300*180
    createCanvas(300, 180);
}

function draw() {
    
    // Set the background color
    background(220);
    
    // Initialize color mode to HSB
    colorMode(HSB, 255);
    
    // Initialize the parameter
    let c = color(56, 126, 10);
    
    // Sets 'value' to 126
    let value = saturation(c); 
    
    // Fill the color
    fill(value);
    
    // Create rectangle
    rect(50, 15, 35, 70);

    // Display result
    text("Value of Saturation is : "
            + int(value), 22, 110);
}
Output: Reference: https://p5js.org/reference/#/p5/saturation

Next Article

Similar Reads