Reference p5.Color

p5.Color

A class to describe a color.

Each p5.Color object stores the color mode and level maxes that were active during its construction. These values are used to interpret the arguments passed to the object's constructor. They also determine output formatting such as when saturation() is called.

Color is stored internally as an array of ideal RGBA values in floating point form, normalized from 0 to 1. These values are used to calculate the closest screen colors, which are RGBA levels from 0 to 255. Screen colors are sent to the renderer.

When different color representations are calculated, the results are cached for performance. These values are normalized, floating-point numbers.

Note: color() is the recommended way to create an instance of this class.

Syntax

p5.Color([pInst], vals)

Parameters

pInst

pointer to p5 instance.

vals

an array containing the color values for red, green, blue and alpha channel or CSS color.

Methods

toString

Returns the color formatted as a String.

Calling myColor.toString() can be useful for debugging, as in print(myColor.toString()). It's also helpful for using p5.js with other libraries.

The parameter, format, is optional. If a format string is passed, as in myColor.toString('#rrggbb'), it will determine how the color string is formatted. By default, color strings are formatted as 'rgba(r, g, b, a)'.

setRed

Sets the red component of a color.

The range depends on the colorMode(). In the default RGB mode it's between 0 and 255.

setGreen

Sets the green component of a color.

The range depends on the colorMode(). In the default RGB mode it's between 0 and 255.

setBlue

Sets the blue component of a color.

The range depends on the colorMode(). In the default RGB mode it's between 0 and 255.

setAlpha

Sets the alpha (transparency) value of a color.

The range depends on the colorMode(). In the default RGB mode it's between 0 and 255.

Notice any errors or typos? Please let us know. Please feel free to edit src/color/p5.Color.js and open a pull request!

Related References