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
pointer to p5 instance.
an array containing the color values for red, green, blue and alpha channel or CSS color.
Methods
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)'
.
Sets the red component of a color.
The range depends on the colorMode(). In the default RGB mode it's between 0 and 255.
Sets the green component of a color.
The range depends on the colorMode(). In the default RGB mode it's between 0 and 255.
Sets the blue component of a color.
The range depends on the colorMode(). In the default RGB mode it's between 0 and 255.
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.