Wednesday, October 8, 2025
HomeLanguagesJavascriptp5.js color() Function

p5.js color() Function

The color() function is used to create color and store it into variables. The parameters of color function are RGB or HSB value depending on the current colorMode() function. The default mode of color() function is RGB value from 0 to 255. Therefore, the function color(255, 204, 0) will return a bright yellow color.

Syntax:

color(gray, alpha)
color(v1, v2, v3, alpha)
color(value)
color(values)
color(color)

Parameters:

  • gray: It is used to set the gray value between white and black.
  • alpha: It is used to set the transparency of the drawing.
  • v1: It is used to set the red or hue value relative to current color range.
  • v2: It is used to set the green or saturation value relative to current color range.
  • v3: It is used to set the blue or brightness value relative to current color range.
  • value: It is used to set the value of color string.
  • values: It is an array containing the red, green, blue and alpha value.
  • color: It is used to set the stroke color.

Return Value: It returns the resulting color value.

Example 1:




function setup() { 
      
    // Create Canvas of given size 
    createCanvas(400, 300); 
  
} 
  
function draw() { 
      
    background(220);
      
    // Use color() function
    let c = color('green');
  
    // Use fill() function to fill color
    fill(c);
    
    // Draw a circle 
    circle(200, 150, 150); 
    
} 


Output:

Example 2:




function setup() { 
      
    // Create Canvas of given size 
    createCanvas(400, 300); 
} 
  
function draw() { 
      
    // Set the background color 
    background(220); 
      
    // Use color() function
    let c = color(0, 155, 0);
      
    // Use fill() function to fill color
    fill(c)
    
    // Draw a line 
    rect(50, 50, 250, 150); 
  
} 


Output:

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

RELATED ARTICLES

Most Popular

Dominic
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6712 POSTS0 COMMENTS
Nicole Veronica
11875 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6833 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS