Skip to content

Color

An RGBA color value.

Colors appear on materials, rendering options, and styles. Each channel (red, green, blue, alpha) is an integer from 0 to 255. You’ll most often encounter Color objects when reading material properties, but you can also construct them directly — for example, to pass into an operation that sets a material’s color.

// Read the color of a material
let model = await SketchUpApi.getActiveModel();
let materials = await model.getMaterials();
let mat = materials.values[0];
console.log(mat.color.toHex());
// => "#FFB299FF"
// Construct a solid red Color
let red = new SketchUpApi.Color(255, 0, 0);
console.log(red.toHex());
// => "#FF0000FF"
// Create a color from a hex string
let pink = SketchUpApi.Color.fromHex('#FF69B4');
console.log(pink.red, pink.green, pink.blue);
// => 255 105 180

new Color(red, green, blue, alpha?): Color

Creates a new Color from individual RGBA channel values.

Parameter Type Default value Description

red

number

undefined

Integer 0–255 representing the red channel, like 255.

green

number

undefined

Integer 0–255 representing the green channel, like 99.

blue

number

undefined

Integer 0–255 representing the blue channel, like 71.

alpha

number

255

Integer 0–255 representing opacity. Defaults to 255 (fully opaque).

Color

// Construct a solid red Color
let red = new SketchUpApi.Color(255, 0, 0);
console.log(red.toHex());
// => "#FF0000FF"
// With custom alpha (semi-transparent)
let ghost = new SketchUpApi.Color(255, 255, 255, 128);
console.log(ghost.alpha);
// => 128

readonly alpha: number = 255

Integer 0–255 representing opacity. Defaults to 255 (fully opaque).


readonly blue: number

Integer 0–255 representing the blue channel, like 71.


readonly green: number

Integer 0–255 representing the green channel, like 99.


readonly red: number

Integer 0–255 representing the red channel, like 255.

toHex(): string

Converts this color to an 8-digit hex string. Always returns the full #RRGGBBAA format, uppercased.

string

The hex representation, like "#FF6347FF".

let c = new SketchUpApi.Color(255, 99, 71, 255);
console.log(c.toHex()); // => "#FF6347FF"

toNumber(): number

Encodes this color as a single 32-bit integer. You typically won’t use this for common extensions, but it’s here if you want to pack an entire color into a single number.

number

let red = new SketchUpApi.Color(255, 0, 0, 255);
console.log(red.toNumber()); // => 4278190335

toString(): string

string


static fromHex(hex): Color

Creates a Color from a hex string. Accepts 3-digit (#RGB), 6-digit (#RRGGBB), or 8-digit (#RRGGBBAA) formats, with or without the leading #.

Parameter Type Description

hex

string

A hex color string, like "#FF6347" or "3CF".

Color

A new Color instance.

let red = SketchUpApi.Color.fromHex('#FF0000');
let semi = SketchUpApi.Color.fromHex('#FF000080');
console.log(semi.alpha); // => 128

Error on invalid input.