Colors
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:
colorize
Section titled “colorize”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.colorsconst { sphere } = jscad.primitivesasync 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:
colorNameToRgb
Section titled “colorNameToRgb”function colorNameToRgb(name: string): number[] | undefinedLook 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.colorsconst { cylinder } = jscad.primitivesasync function main() { return colorize(colorNameToRgb('coral'), cylinder())}See also:
hexToRgb
Section titled “hexToRgb”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.colorsconst { sphere } = jscad.primitivesasync function main() { return colorize(hexToRgb('#000080'), sphere())}See also:
hslToRgb
Section titled “hslToRgb”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.colorsconst { sphere } = jscad.primitivesasync function main() { // Hue 0.33 ≈ green, full saturation, 50% lightness return colorize(hslToRgb(0.33, 1, 0.5), sphere())}See also:
hsvToRgb
Section titled “hsvToRgb”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.colorsconst { sphere } = jscad.primitivesasync function main() { // Hue 0.6 ≈ blue, full saturation and brightness return colorize(hsvToRgb(0.6, 1, 1), sphere())}See also:
rgbToHex
Section titled “rgbToHex”function rgbToHex(...values: number[]): stringConvert 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.colorsconst hex = rgbToHex(...colorNameToRgb('steelblue')) // '#4682b4'See also: