Skip to content

Color assignment and color space conversion for JSCAD geometries. Functions accept and return normalized color arrays where each channel is in [0, 1]. Geometries are not mutated — colored copies are returned.

See also:

function colorize(color: number[], ...objects: object[]): object | object[]

Assign a color to one or more JSCAD geometries.

All channel values must be in the range [0, 1]. If only three values are provided the alpha defaults to 1 (fully opaque). The original geometry is not mutated — a new colored copy is returned.

Works with geom2, geom3, and path2 objects. Passing multiple geometries (or a flat array) returns a matching array of colored copies.

Parameters:

Name Type Description
color — RGBA color as [r, g, b] or [r, g, b, a], each value 0–1.
objects — One or more JSCAD geometries to colorize.

Returns: Colored copy of the input geometry, or an array of copies.

const { colorize, colorNameToRgb } = jscad.colors
const { sphere } = jscad.primitives
async function main() {
const red = colorize([1, 0, 0], sphere())
const tinted = colorize([0.2, 0.8, 0.4, 0.7], sphere({ radius: 2 }))
const named = colorize(colorNameToRgb('steelblue'), sphere({ radius: 3 }))
return [red, tinted, named]
}

See also:

function colorNameToRgb(name: string): number[] | undefined

Look up a CSS color name and return its RGB values.

Supports all 148 CSS named colors (e.g. ‘red’, ‘steelblue’, ‘fuchsia’). The name is case-insensitive. Returns undefined if the name is not recognised.

Parameters:

Name Type Description
name — CSS color name (e.g. ‘red’, ‘lime’, ‘steelblue’).

Returns: [r, g, b] array with values in [0, 1], or undefined if not found.

const { colorize, colorNameToRgb } = jscad.colors
const { cylinder } = jscad.primitives
async function main() {
return colorize(colorNameToRgb('coral'), cylinder())
}

See also:

function hexToRgb(notation: string): number[]

Parse a CSS hex color string into an RGB or RGBA array.

Accepts 6-digit (#rrggbb) and 8-digit (#rrggbbaa) hex strings. The ‘#’ prefix is optional. All values in the returned array are in [0, 1].

Parameters:

Name Type Description
notation — Hex color string, e.g. ‘#ff8800’ or ‘ff8800ff’.

Returns: [r, g, b] or [r, g, b, a] with values in [0, 1].

const { colorize, hexToRgb } = jscad.colors
const { sphere } = jscad.primitives
async function main() {
return colorize(hexToRgb('#000080'), sphere())
}

See also:

function hslToRgb(...values: number[]): number[]

Convert HSL (hue, saturation, lightness) color values to an RGB array.

Each component is in [0, 1]. Accepts values as individual arguments or a flat array. Pass a fourth value for alpha (HSLA → RGBA).

Parameters:

Name Type Description
values — H, S, L, and optionally A values in [0, 1].

Returns: [r, g, b] or [r, g, b, a] with values in [0, 1].

const { colorize, hslToRgb } = jscad.colors
const { sphere } = jscad.primitives
async function main() {
// Hue 0.33 ≈ green, full saturation, 50% lightness
return colorize(hslToRgb(0.33, 1, 0.5), sphere())
}

See also:

function hsvToRgb(...values: number[]): number[]

Convert HSV (hue, saturation, value/brightness) color values to an RGB array.

Each component is in [0, 1]. Accepts values as individual arguments or a flat array. Pass a fourth value for alpha (HSVA → RGBA).

Parameters:

Name Type Description
values — H, S, V, and optionally A values in [0, 1].

Returns: [r, g, b] or [r, g, b, a] with values in [0, 1].

const { colorize, hsvToRgb } = jscad.colors
const { sphere } = jscad.primitives
async function main() {
// Hue 0.6 ≈ blue, full saturation and brightness
return colorize(hsvToRgb(0.6, 1, 1), sphere())
}

See also:

function rgbToHex(...values: number[]): string

Convert an RGB or RGBA color array to a CSS hex color string.

Accepts values as individual arguments or a flat array. Each component must be in [0, 1]. Returns a 6-digit string (#rrggbb) for RGB, or an 8-digit string (#rrggbbaa) when alpha is included.

Parameters:

Name Type Description
values — R, G, B, and optionally A values in [0, 1].

Returns: CSS hex color string (e.g. ‘#ff8800’ or ‘#ff880080’).

const { rgbToHex, colorNameToRgb } = jscad.colors
const hex = rgbToHex(...colorNameToRgb('steelblue')) // '#4682b4'

See also: