An RGBA color.
Each color component is a floating point value in the range 0 to 1. The r (red), g (green)
and b (blue) components define a color in RGB color space. The a (alpha) component defines
transparency. An alpha of 1 is fully opaque. An alpha of 0 is fully transparent.
new Color(
r?: number | number[],
g?: number,
b?: number,
a?: number): Color
Create a new Color object.
The value of the red component (0-1). Defaults to 0. If r is an array of length 3 or 4, the array will be used to populate all components.
number | number
number = 0
The value of the green component (0-1). Defaults to 0.
number = 0
The value of the blue component (0-1). Defaults to 0.
number = 1
The value of the alpha component (0-1). Defaults to 1.
a: number
The alpha component of the color.
b: number
The blue component of the color.
g: number
The green component of the color.
r: number
The red component of the color.
readonly static BLACK: Color;
A constant color set to black 0, 0, 0, 1.
readonly static BLUE: Color;
A constant color set to blue 0, 0, 1, 1.
readonly static CYAN: Color;
A constant color set to cyan 0, 1, 1, 1.
readonly static GRAY: Color;
A constant color set to gray 0.5, 0.5, 0.5, 1.
readonly static GREEN: Color;
A constant color set to green 0, 1, 0, 1.
readonly static MAGENTA: Color;
A constant color set to magenta 1, 0, 1, 1.
readonly static RED: Color;
A constant color set to red 1, 0, 0, 1.
readonly static WHITE: Color;
A constant color set to white 1, 1, 1, 1.
readonly static YELLOW: Color;
A constant color set to yellow 1, 1, 0, 1.
clone(): Color
Returns a clone of the specified color.
A duplicate color object.
copy(rhs: Color): Color
Copies the contents of a source color to a destination color.
A color to copy to the specified color.
Self for chaining.
const src = new Color(1, 0, 0, 1)
const dst = new Color()
dst.copy(src)
console.log('The two colors are ' + (dst.equals(src) ? 'equal' : 'different'))
equals(rhs: Color): boolean
Reports whether two colors are equal.
The color to compare to the specified color.
boolean
True if the colors are equal and false otherwise.
const a = new Color(1, 0, 0, 1)
const b = new Color(1, 1, 0, 1)
console.log('The two colors are ' + (a.equals(b) ? 'equal' : 'different'))
fromArray(arr: number[], offset?: number): Color
Set the values of the vector from an array.
number
The array to set the vector values from.
number = 0
The zero-based index at which to start copying elements from the array. Default is 0.
Self for chaining.
const c = new Color()
c.fromArray([
1,
0,
1,
1
])
// c is set to [1, 0, 1, 1]
fromString(hex: string): Color
Set the values of the color from a string representation '#11223344' or '#112233'.
string
A string representation in the format '#RRGGBBAA' or '#RRGGBB'. Where RR, GG, BB, AA are red, green, blue and alpha values. This is the same format used in HTML/CSS.
Self for chaining.
gamma(src?: Color): Color
Converts the color from linear to gamma color space.
Color = ...
The color to convert to gamma color space. If not set, the operation is done in place.
Self for chaining.
lerp(
lhs: Color,
rhs: Color,
alpha: number): Color
Returns the result of a linear interpolation between two specified colors.
The color to interpolate from.
The color to interpolate to.
number
The value controlling the point of interpolation. Between 0 and 1, the linear interpolant will occur on a straight line between lhs and rhs. Outside of this range, the linear interpolant will occur on a ray extrapolated from this line.
Self for chaining.
const a = new Color(0, 0, 0)
const b = new Color(1, 1, 0.5)
const r = new Color()
r.lerp(a, b, 0) // r is equal to a
r.lerp(a, b, 0.5) // r is 0.5, 0.5, 0.25
r.lerp(a, b, 1) // r is equal to b
linear(src?: Color): Color
Converts the color from gamma to linear color space.
Color = ...
The color to convert to linear color space. If not set, the operation is done in place.
Self for chaining.
mulScalar(scalar: number): Color
Multiplies RGB elements of a Color by a number. Note that the alpha value is left unchanged.
number
The number to multiply by.
Self for chaining.
set(
r: number,
g: number,
b: number,
a?: number): Color
Assign values to the color components, including alpha.
number
The value for red (0-1).
number
The value for blue (0-1).
number
The value for green (0-1).
number = 1
The value for the alpha (0-1), defaults to 1.
Self for chaining.
toArray(
arr?: number[],
offset?: number,
alpha?: boolean): number[]
Converts the color to an array of numbers.
number = []
The array to populate with the color components. If not specified, a new array is created. Default is true.
number = 0
The zero-based index at which to start copying elements to the array. Default is 0.
boolean = true
If true, the output array will include the alpha value.
number
The color as an array of numbers.
const c = new Color(1, 1, 1)
// Outputs [1, 1, 1, 1]
console.log(c.toArray())
toString(alpha: boolean, asArray?: boolean): string
Converts the color to string form. The format is '#RRGGBBAA', where RR, GG, BB, AA are the red, green, blue and alpha values. When the alpha value is not included (the default), this is the same format as used in HTML/CSS.
boolean
If true, the output string will include the alpha value.
boolean
If true, the output will be an array of numbers. Defaults to false.
string
The color in string form.
const c = new Color(1, 1, 1)
// Outputs #ffffffff
console.log(c.toString())