Name

color()

Description

Creates colors for storing in variables of the color datatype. The parameters are interpreted as RGB or HSB values depending on the current colorMode(). The default mode is RGB values from 0 to 255 and, therefore, color(255, 204, 0) will return a bright yellow color (see the first example above).

Note that if only one value is provided to color(), it will be interpreted as a grayscale value. Add a second value, and it will be used for alpha transparency. When three values are specified, they are interpreted as either RGB or HSB values. Adding a fourth value applies alpha transparency.

Note that when using hexadecimal notation, it is not necessary to use color(), as in: color c = #006699

More about how colors are stored can be found in the reference for the color datatype.

Examples

  • size(400,400);
    color c = color(255, 204, 0);  // Define color 'c'
    fill(c);  // Use color variable 'c' as fill color
    noStroke();  // Don't draw a stroke around shapes
    rect(120, 80, 220, 220);  // Draw rectangle
    Image output for example 1
  • size(400,400);
    color c = color(255, 204, 0);  // Define color 'c'
    fill(c);  // Use color variable 'c' as fill color
    noStroke();  // Don't draw a stroke around shapes
    ellipse(100, 100, 320, 320);  // Draw left circle
    
    // Using only one value with color()
    // generates a grayscale value.
    c = color(65);  // Update 'c' with grayscale value
    fill(c);  // Use updated 'c' as fill color
    ellipse(300, 300, 320, 320);  // Draw right circle
    Image output for example 2
  • size(400,400);
    color c;  // Declare color 'c'
    noStroke();  // Don't draw a stroke around shapes
    
    // If no colorMode is specified, then the
    // default of RGB with scale of 0-255 is used.
    c = color(50, 55, 100);  // Create a color for 'c'
    fill(c);  // Use color variable 'c' as fill color
    rect(0, 40, 180, 320);  // Draw left rect
    
    colorMode(HSB, 100);  // Use HSB with scale of 0-100
    c = color(50, 55, 100);  // Update 'c' with new color
    fill(c);  // Use updated 'c' as fill color
    rect(220, 40, 180, 320);  // Draw right rect
    Image output for example 3

Syntax

  • color(gray)
  • color(gray, alpha)
  • color(v1, v2, v3)
  • color(v1, v2, v3, alpha)

Parameters

  • gray(int)number specifying value between white and black
  • alpha(int, float)relative to current color range
  • v1(int, float)red or hue values relative to the current color range
  • v2(int, float)green or saturation values relative to the current color range
  • v3(int, float)blue or brightness values relative to the current color range

Return

  • int