Skip to content

color.parse

Summary

color.parse converts hex and comma-separated RGB(A) colors from sRGB to the linear RGBA expected by material and geometry color inputs. Use it when a tint comes from CSS or a color picker. The RGB channels are decoded and the alpha fraction is carried through.

Syntax

parse(css: string): Color4
const tint = WasmGPU.color.parse("#5588ffaa");

Parameters

Name Type Required Description
css string Yes Hex #rgb, #rgba, #rrggbb or #rrggbbaa, or comma-separated rgb(r, g, b) or rgba(r, g, b, a). RGB channels in functional notation are integers from 0 to 255. Alpha is a number in [0, 1].

Returns

Color4 - A fresh [r, g, b, a] tuple with linear RGB. Alpha defaults to 1. Unsupported syntax or out-of-range components throw. Named colors, percentages and space-separated CSS forms are unsupported. The package also exports the function as parseColor.

Example

import { parseColor } from "@zushah/wasmgpu";

const tint = parseColor("rgba(128, 32, 255, 0.5)");
console.log(tint); // Linear red is about 0.216

See Also