Friday, September 19, 2025
HomeLanguagesJavascriptp5.js alpha() function

p5.js alpha() function

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

Syntax:

alpha(c)

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

  • c : This parameter stores the p5.Color object, color components, or CSS color.
  • Below program illustrates the alpha() function in p5.js:
    Example-1:




    function setup() {
        
        //create Canvas of size 300*80  
        createCanvas(300, 80);
    }
      
    function draw() {
        background(220);
        
        //initialize the parameter  
        let c = color(0, 126, 255, 102);
        
        //extract the alpha value
        let y = alpha(c);
        textSize(16);
        fill(color('red'));
        text("Alpha Value is : " + y, 50, 30);
    }

    
    

    Output:

    Example-2:




    function setup() {
        
        //create Canvas of size 300*80  
        createCanvas(160, 180);
    }
      
    function draw() {
        background(220);
        
        //initialize the parameter  
        let c = color(0, 126, 255, 34);
        
        // Sets 'value' to 34
        let value = alpha(c);
        fill(value);
        rect(50, 15, 35, 70);
        text("Value of alpha is : " + value, 22, 110);
    }

    
    

    Output:

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

    RELATED ARTICLES

    Most Popular

    Dominic
    32301 POSTS0 COMMENTS
    Milvus
    84 POSTS0 COMMENTS
    Nango Kala
    6666 POSTS0 COMMENTS
    Nicole Veronica
    11840 POSTS0 COMMENTS
    Nokonwaba Nkukhwana
    11898 POSTS0 COMMENTS
    Shaida Kate Naidoo
    6781 POSTS0 COMMENTS
    Ted Musemwa
    7056 POSTS0 COMMENTS
    Thapelo Manthata
    6739 POSTS0 COMMENTS
    Umr Jansen
    6745 POSTS0 COMMENTS