Skip to main content
Version: Phaser v4.0.0

ColorRamp

The ColorRamp class represents a series of color transitions. It is intended for use in a {@see Phaser.GameObjects.Gradient}.

You should make sure that your bands are arranged end-to-end, with no gaps. The Gradient shader assumes this is so. You may leave gaps at the start and end. Overlaps and gaps may not act as expected.

By default, ColorRamp stores its data for use on the GPU in a data texture. This is updated automatically on creation and when you run setBands(), but if you edit the bands manually, you should run encode() to rebuild the texture. We don't update it automatically because we don't want to waste cycles on rebuilds that you're about to overwrite.

Constructor

new ColorRamp(scene, bands, [gpuEncode])

Parameters

nametypeoptionaldefaultdescription
scenePhaser.SceneNoThe current scene.
bandsPhaser.Types.Display.ColorBandConfig | Phaser.Display.ColorBandArray.<(Phaser.Types.Display.ColorBandConfigPhaser.Display.ColorBand)>No
gpuEncodebooleanYestrueWhether to create a data texture to use this ramp in shaders.

Scope: static

Source: src/display/ColorRamp.js#L15
Since: 4.0.0

Public Members

bands

bands: Array.<Phaser.Display.ColorBand>

Description:

The color bands that make up this ramp.

Source: src/display/ColorRamp.js#L56
Since: 4.0.0


bandTreeDepth

bandTreeDepth: number

Description:

The number of levels in the band tree. This is used internally to decode the data texture.

Source: src/display/ColorRamp.js#L112
Since: 4.0.0


dataTexture

dataTexture: Phaser.Textures.Texture

Description:

The Phaser Texture wrapping the GPU data texture for this ramp. This is registered with the Scene's Texture Manager under a unique key so it can be referenced elsewhere. It is not intended for display.

Source: src/display/ColorRamp.js#L76
Since: 4.0.0


dataTextureFirstBand

dataTextureFirstBand: number

Description:

The texel index which contains the first band data in glTexture if it has been encoded. This is used internally.

Source: src/display/ColorRamp.js#L100
Since: 4.0.0


dataTextureResolution

dataTextureResolution: Array.<number>

Description:

The resolution of the data texture, if it has been encoded. This is used internally.

Source: src/display/ColorRamp.js#L123
Since: 4.0.0


glTexture

glTexture: Phaser.Renderer.WebGL.Wrappers.WebGLTextureWrapper

Description:

The texture containing the ramp encoded for the GPU. This is used internally by effects such as the Gradient game object to read complex ramp data.

Source: src/display/ColorRamp.js#L88
Since: 4.0.0


gpuEncode

gpuEncode: boolean

Description:

Whether to encode the ramp for shaders to use on the GPU. An encoded ramp is stored as a texture.

Source: src/display/ColorRamp.js#L65
Since: 4.0.0


scene

scene: Phaser.Scene

Description:

The scene where the ColorRamp was created.

Source: src/display/ColorRamp.js#L46
Since: 4.0.0


Public Methods

destroy

<instance> destroy()

Description:

Destroy this ColorRamp. If it has a data texture, destroy it.

Source: src/display/ColorRamp.js#L497
Since: 4.0.0


encode

<instance> encode()

Description:

Encode a data texture from the color ramp bands.

This process runs automatically when gpuEncode is enabled and the bands are set or updated with setBands. If you modify the bands directly, you must call encode yourself.

The data is encoded in texels as follows:

Numbers are encoded in "RG.BA" form. The number equals R * 255 + G + B / 255 + A / 255 / 255.

  • First 2 texels: start and end.

    • Start is the start of the first band.

    • End is the end of the last band.

  • Next block of texels: binary symmetrical tree of band ranges,

    represented as the end value of the midpoint band.

  • Final block of texels, starting at dataTextureFirstBand:

    the band data, in blocks of 3:

    • colorStart

    • colorEnd

    • colorSpace * 255 + interpolation + (middle / 2)

The binary symmetrical tree breaks the bands list in half with every node. It is intended to quickly find the band corresponding to a given progress along the ramp. For example, a ramp with 10 bands would store the ends in this order:

[ 7, 3, 11, 1, 5, 9, 13, 0, 2, 4, 6, 8, 10, 12, 14 ]

But because it doesn't have bands from index 10 and up, it's actually:

[ 7, 3, 9, 1, 5, 9, 9, 0, 2, 4, 6, 8, 9, 9, 9 ]

Note that, if you change the number of bands in the ramp, dataTexture may no longer have the correct resolution. It is not intended for display.

Source: src/display/ColorRamp.js#L191
Since: 4.0.0


fixFit

<instance> fixFit(start, end, purgeZeroLength, encode)

Description:

Fix the fit of bands within this ColorRamp.

This sets the start of each band to the end of the previous band, ensuring that there are no gaps.

Optionally, you can define start and end values to stretch the ramp to some specific range, e.g. 0-1.

By default, any band that is now 0 length will be removed.

Parameters:

nametypeoptionaldescription
startnumberNoOverride the start of the first band.
endnumberNoOverride the end of the last band.
purgeZeroLengthbooleanNoWhether to discard bands that now have 0 size.
encodebooleanNoWhether to reencode the data texture.

Returns: Phaser.Display.ColorRamp - - This ColorRamp instance.

Source: src/display/ColorRamp.js#L324
Since: 4.0.0


getColor

<instance> getColor(index)

Description:

Get the color value at the given index within this ramp.

If there is no band at that location, the color is transparent.

Parameters:

nametypeoptionaldescription
indexnumberNoIndex of the color to get, from 0 (start) to 1 (end).

Returns: Phaser.Types.Display.ColorObject - The color at that index.

Source: src/display/ColorRamp.js#L462


setBands

<instance> setBands(bands, [encode])

Description:

Set or replace the color bands in this ramp. Use this after creation to update the bands.

This will re-encode the data texture if gpuEncode is set and encode is not false.

Parameters:

nametypeoptionaldefaultdescription
bandsPhaser.Types.Display.ColorBandConfig | Phaser.Display.ColorBandArray.<(Phaser.Types.Display.ColorBandConfigPhaser.Display.ColorBand)>No
encodebooleanYestrueWhether to encode the new ramp data to a data texture for use in shaders.

Returns: Phaser.Display.ColorRamp - - This ColorRamp instance.

Source: src/display/ColorRamp.js#L137
Since: 4.0.0


splitBand

<instance> splitBand(band, steps, [quantize], [encode])

Description:

Split a band from this ramp into several bands, and insert them into the ramp.

You can choose whether to "quantize" the bands, where each of them has a flat color.

Parameters:

nametypeoptionaldefaultdescription
bandnumber | Phaser.Display.ColorBandNoThe band to split, either an index on this ramp or the band instance. The band must be on this ramp.
stepsnumberNoThe number of bands to create.
quantizebooleanYesfalseWhether to quantize the bands to a single color.
encodebooleanYestrueWhether to rebuild the data texture.

Returns: Phaser.Display.ColorRamp - This ColorRamp instance.

Source: src/display/ColorRamp.js#L387
Since: 4.0.0