Skip to content

Class: ColorMap

Defined in: src/display/ColorMap.ts:143

ColorMap is responsible for mapping numerical data to colors (RGB or RGBA). It supports:

  • Setting a [min, max] range that is scaled into the color ramp.
  • Thresholding a [low, high] band for full transparency.
  • Optional alpha array or global alpha for transparency control.

Constructors

Constructor

ts
new ColorMap(colors, options?): ColorMap;

Defined in: src/display/ColorMap.ts:157

Constructs a ColorMap with given colors and optional configuration.

Parameters

colors

Color[]

An array of color entries. Each color is [R, G, B] or [R, G, B, A] in [0..1].

options?

ColorMapOptions = {}

Additional settings for range, threshold, alpha, etc.

Returns

ColorMap

Properties

name

ts
name: string;

Defined in: src/display/ColorMap.ts:149


isCustom

ts
isCustom: boolean;

Defined in: src/display/ColorMap.ts:150


GRAY_SCALE

ts
readonly static GRAY_SCALE: ColorMap;

Defined in: src/display/ColorMap.ts:189

A built-in static ColorMap instance from black to white. This is commonly used as a baseline or fallback map.

Accessors

hasAlpha

Get Signature

ts
get hasAlpha(): boolean;

Defined in: src/display/ColorMap.ts:538

Indicates whether this color map has an alpha channel (based on the first color or forced by setAlpha()).

Returns

boolean


presetMaps

Get Signature

ts
get static presetMaps(): object;

Defined in: src/display/ColorMap.ts:575

Lazy getter for preset colormaps, returning a dictionary from preset name to array of hex color strings. We also add "Grayscale" as a known preset.

Returns

object

Methods

on()

ts
on<K>(type, handler): void;

Defined in: src/display/ColorMap.ts:199

Subscribe to color map events such as "rangeChanged," "thresholdChanged," or "alphaChanged."

Type Parameters

K

K extends keyof ColorMapEvents

Parameters

type

K

The event name, e.g. 'rangeChanged'.

handler

(args) => void

Function to call when the event fires.

Returns

void


off()

ts
off<K>(type, handler): void;

Defined in: src/display/ColorMap.ts:208

Unsubscribe from color map events.

Type Parameters

K

K extends keyof ColorMapEvents

Parameters

type

K

The event name.

handler

(args) => void

The specific function to remove.

Returns

void


getColorMap()

ts
getColorMap(): Color[];

Defined in: src/display/ColorMap.ts:216

Returns the raw internal array of color stops used for mapping. Useful if you want to examine or iterate over the color map in detail.

Returns

Color[]


setRange()

ts
setRange(range?): void;

Defined in: src/display/ColorMap.ts:225

Sets the data range [min, max] for color mapping. Data below min maps to the first color, data above max maps to the last color.

Parameters

range?

[number, number]

The [min, max] values; if invalid, defaults to [0, 1].

Returns

void


setThreshold()

ts
setThreshold(threshold?): void;

Defined in: src/display/ColorMap.ts:243

Sets the threshold values [low, high]. Data strictly between low and high is fully transparent, outside that region is fully visible. If low >= high, thresholding is disabled.

Parameters

threshold?

[number, number]

The new threshold values; default [0, 0] (no threshold).

Returns

void


getRange()

ts
getRange(): [number, number];

Defined in: src/display/ColorMap.ts:260

Gets the current range

Returns

[number, number]


getThreshold()

ts
getThreshold(): [number, number];

Defined in: src/display/ColorMap.ts:267

Gets the current threshold

Returns

[number, number]


setAlpha()

ts
setAlpha(alpha?): void;

Defined in: src/display/ColorMap.ts:312

Sets a global alpha or per-color alpha array. Rebuilds the internal color array to ensure alpha is included if any is provided.

Parameters

alpha?

number | number[]

Either a single alpha in [0..1] or an array of length = number of colors, or undefined to preserve the current alpha.

Returns

void


getColor()

ts
getColor(value): Color;

Defined in: src/display/ColorMap.ts:350

Maps a single numeric data value to a color, applying range scaling and threshold-based transparency if present.

Parameters

value

number

The numeric value to map.

Returns

Color

An [R, G, B] or [R, G, B, A] color (in [0..1]).


fillImageData()

ts
fillImageData(imageData, values): ImageData;

Defined in: src/display/ColorMap.ts:404

Fills a provided ImageData object with RGBA values derived from this color map.

Parameters

imageData

ImageData

An ImageData object to fill.

values

number[] | NumericTypedArray

A numerical array or TypedArray of the same length as the number of pixels in imageData.

Returns

ImageData

The modified ImageData (same object).


getColorArray()

ts
getColorArray(values): Float32Array;

Defined in: src/display/ColorMap.ts:463

Maps a numeric array to a Float32Array of color components (RGB or RGBA). This is often used for GPU-based rendering or custom shader pipelines.

Parameters

values

number[] | Float32Array<ArrayBufferLike>

An array or Float32Array of scalar values.

Returns

Float32Array

A Float32Array of length = values.length * (3 or 4).


getColors()

ts
getColors(values): Color[];

Defined in: src/display/ColorMap.ts:527

Returns an array of color entries for each data value in the input array, i.e., calling getColor() on each value in sequence.

Parameters

values

number[]

A numeric array to map.

Returns

Color[]

An array of Colors in [0..1].


length()

ts
length(): number;

Defined in: src/display/ColorMap.ts:545

Returns the number of color stops in this color map.

Returns

number


copy()

ts
copy(newColors, newName?): ColorMap;

Defined in: src/display/ColorMap.ts:555

Creates a new ColorMap as a copy of the current one but with a changed color array or name. Range, threshold, etc. are duplicated from the original.

Parameters

newColors

Color[]

The array of new color stops.

newName?

string

Optional new name. Defaults to something like "<oldName> (Copy)".

Returns

ColorMap


generatePreset()

ts
static generatePreset(name): string[];

Defined in: src/display/ColorMap.ts:608

Generates a 256-color scale from the named ColorBrewer scale if available, else defaults to a black-white scale.

Parameters

name

string

The scale name, e.g., "Viridis" or "RdYlBu".

Returns

string[]


getAvailableMaps()

ts
static getAvailableMaps(): string[];

Defined in: src/display/ColorMap.ts:623

Retrieves a list of all known preset map names. This includes both official ColorBrewer sets and the "Grayscale" fallback.

Returns

string[]


fromPreset()

ts
static fromPreset(name, options?): ColorMap;

Defined in: src/display/ColorMap.ts:634

Returns a new ColorMap using a named preset. If "existingColorMap" is provided and name is "Custom", a copy is created from the existing color map's colors.

Parameters

name

string

The preset name (must exist in ColorMap.presetMaps).

options?

ColorMapOptions & object = {}

Additional options. If 'existingColorMap' is provided with name="Custom", a direct copy is performed.

Returns

ColorMap

Released under the MIT License.