rgb()

Category:Drawing

Using RGBA values, creates colors for using in the background, shapes, lines and points.

Explore the 256^3 possible colors you can choose from using three numeric parameters.

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

Examples

Flag of France

You can look up official RGB values for most country flags on Wikipedia.

// You can look up official RGB values for most country flags on Wikipedia.
fill(rgb(0,85,164));
rect(50, 50, 300, 100);

fill(rgb(255,255,255));
rect(50, 150, 300, 100);

fill(rgb(239,65,53));
rect(50, 250, 300, 100);

// one parameter is grayscale
fill(rgb(100));  
rect(100, 100, 50, 50);

// two parameters is grayscale and transparency
fill(rgb(100,0.5)); 
rect(150, 100, 50, 50);

// three parameters are RGB values
fill(rgb(0,100,200));
rect(100, 150, 50, 50);

// four parameters are RGB values and transparency
fill(rgb(0, 100, 200, 1));
rect(150, 150, 50, 50);

Syntax

rgb(r, g, b, a)

Parameters

NameTypeRequired?Description
r Number The amount of red (0-255) in the color, or if the only parameter a grayscale value.
g Number The amount of green (0-255) in the color, or if the last parameter the alpha transparency for grayscale
b Number The amount of blue (0-255) in the color.
a Number The alpha transparency, a number between 0 (fully transparent) and 1(fully opaque), of the color. Default is 1.

Returns

Returns a color. Usually used as a parameter to background(), fill(), or stroke().

Tips

  • If any of the RGBA values is out of range (0-255), then the value is rounded into range. For example, 300 becomes 255 and -100 becomes 0.

Found a bug in the documentation? Let us know at documentation@code.org