colors, floatColors, and storageColors
The colors shader module provides shared utilities for semantic colors in shader code.
floatColors is the legacy helper-name alias retained for callers that already import
floatColors_* functions. storageColors is the separate WGSL storage-buffer reader for packed
RGBA rows.
Props​
useByteColors?: boolean​
When true, semantic colors passed through colors_normalize() or floatColors_normalize() are interpreted as
byte-style 0..255 values and divided by 255.0 in shader code. When false, semantic
colors are interpreted directly as floats and are returned unchanged.
This is intended for semantic colors only. Raw texel storage such as Uint8Array texture
uploads remains byte-oriented.
Shader Functions​
colors_normalize(vec3 inputColor) / floatColors_normalize(vec3 inputColor)​
Normalizes byte-style RGB colors to 0..1 when useByteColors is enabled. Float colors pass through.
colors_normalize(vec4 inputColor) / colors_normalize4(vec4<f32>)​
GLSL uses colors_normalize(vec4) while WGSL uses colors_normalize4(vec4<f32>).
The legacy floatColors namespace follows the same GLSL/WGSL naming split.
Byte-style RGBA colors normalize to 0..1 when useByteColors is enabled.
Float colors pass through.
colors_premultiplyAlpha(vec4 inputColor) / floatColors_premultiplyAlpha(vec4 inputColor)​
Returns vec4(inputColor.rgb * inputColor.a, inputColor.a).
colors_unpremultiplyAlpha(vec4 inputColor) / floatColors_unpremultiplyAlpha(vec4 inputColor)​
Returns the unpremultiplied RGBA color when alpha is greater than zero, otherwise returns transparent black.
Usage​
import {colors, lighting} from '@luma.gl/shaders/modules';
const shaderInputs = new ShaderInputs({colors, lighting});
shaderInputs.setProps({
colors: {useByteColors: false},
lighting: {
useByteColors: false,
lights: [{type: 'ambient', color: [1, 0.9, 0.8], intensity: 0.2}]
}
});
var surfaceColor = colors_normalize4(vec4<f32>(255.0, 128.0, 64.0, 255.0));
surfaceColor = colors_premultiplyAlpha(surfaceColor);
Packed Storage Colors​
storageColors reads packed read-only storage rows in WGSL. It accepts
rgba8unorm, rgba16float, and rgba32float rows, represented by
STORAGE_COLOR_FORMAT numeric tags plus 4-byte aligned byteOffset and
byteStride props.
import {storageColors} from '@luma.gl/shaders/modules';
const shaderInputs = new ShaderInputs({storageColors});
shaderInputs.setProps({
storageColors: {
colorBuffer,
format: 'rgba8unorm',
byteStride: 4
}
});
let color = storageColors_readColor(instanceIndex);
storageColors_readColor() returns normalized Float32 RGBA values. The storage
reader does not implicitly bind colors; include both modules when a shader
also needs semantic color normalization helpers.
Remarks​
colorsandfloatColorsare for semantic colors, not packed image data.floatColorspreserves legacy helper names; new WGSL code should prefercolors.storageColorsis WGSL-only because it reads read-only storage buffers.- Packed vertex colors still work well with normalized vertex formats such as
unorm8x4. - Phase 1 compatibility defaults are preserved on the APIs that adopt
useByteColors.
JavaScript color helpers​
normalizeByteColor3(color, useByteColors = true) and
normalizeByteColor4(color, useByteColors = true) decode CPU color values:
| Input | Interpretation |
|---|---|
| JavaScript array | Bytes by default; normalized when useByteColors is false |
Uint8Array, Uint8ClampedArray | Bytes, regardless of the flag |
Native Float16Array, Float32Array, Float64Array | Normalized, regardless of the flag |
The RGB helper returns three components; the RGBA helper returns four. Missing or non-finite alpha becomes opaque (255 for byte input, 1 for normalized input). Explicit zero remains zero. Neither helper clamps values, changes color space, nor premultiplies alpha.
Exactly sized floating-point inputs are returned by reference, with no allocation, unless
RGBA alpha needs repair. Treat these results as borrowed storage. Other inputs produce a new
JavaScript array, without mutating the original. Raw half-float bits in Uint16Array are not
supported; accepting native Float16 values here does not guarantee that a particular GPU upload
API accepts Float16 arrays.
const bytes = normalizeByteColor4(new Uint8Array([255, 128, 0])); // [1, 128/255, 0, 1]
const floats = new Float32Array([1, 0.5, 0, 0]);
normalizeByteColor4(floats) === floats; // true: transparent alpha is preserved