Skip to content

Color ​

Immutable RGBA color with components in [0, 1].

Color represents an RGBA value with four components. It is used for channel tints, label color maps, and wireframe overlays. Components are validated at construction and never change. Every API that takes a color accepts a ColorLike so hex strings and component arrays coerce automatically through from. Common colors are available as static presets.

Constructor ​

Creates a color from RGBA components in [0, 1].

ts
new Color(r, g, b, a?): Color;
ParameterDescription
r: numberThe red component.
g: numberThe green component.
b: numberThe blue component.
a?: numberThe alpha component. Defaults to 1.

Types ​

ColorLike ​

A value convertible to Color.

ts
type ColorLike = Color | vec3 | vec4 | string;

Properties ​

RED ​

Opaque red #ff0000.

ts
readonly static RED: Color;

GREEN ​

Opaque green #00ff00.

ts
readonly static GREEN: Color;

BLUE ​

Opaque blue #0000ff.

ts
readonly static BLUE: Color;

YELLOW ​

Opaque yellow #ffff00.

ts
readonly static YELLOW: Color;

MAGENTA ​

Opaque magenta #ff00ff.

ts
readonly static MAGENTA: Color;

CYAN ​

Opaque cyan #00ffff.

ts
readonly static CYAN: Color;

BLACK ​

Opaque black #000000.

ts
readonly static BLACK: Color;

WHITE ​

Opaque white #ffffff.

ts
readonly static WHITE: Color;

TRANSPARENT ​

Fully transparent black.

ts
readonly static TRANSPARENT: Color;

Accessors ​

rgb [number, number, number] ​

The RGB components as a three-element array.

ts
get rgb(): [number, number, number];

rgba readonly [number, number, number, number] ​

The RGBA components as a four-element array.

ts
get rgba(): readonly [number, number, number, number];

r number ​

The red component.

ts
get r(): number;

g number ​

The green component.

ts
get g(): number;

b number ​

The blue component.

ts
get b(): number;

a number ​

The alpha component.

ts
get a(): number;

rgbHex string ​

The color as a #rrggbb hex string. Alpha is dropped.

ts
get rgbHex(): string;

packed number ​

The color packed into a 32-bit integer as RGBA bytes.

ts
get packed(): number;

Methods ​

from() Color ​

Converts a ColorLike value to a Color.

ts
static from(colorLike): Color;
ParameterDescription
colorLike: ColorLikeThe value to convert.

fromRgbHex() Color ​

Parses a #rrggbb hex string into an opaque color.

ts
static fromRgbHex(hex): Color;
ParameterDescription
hex: stringThe hex string with or without the leading #.

Released under the MIT License