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.
Example
Section titled “Example”// Read the color of a materiallet model = await SketchUpApi.getActiveModel();let materials = await model.getMaterials();let mat = materials.values[0];console.log(mat.color.toHex());// => "#FFB299FF"
// Construct a solid red Colorlet red = new SketchUpApi.Color(255, 0, 0);console.log(red.toHex());// => "#FF0000FF"
// Create a color from a hex stringlet pink = SketchUpApi.Color.fromHex('#FF69B4');console.log(pink.red, pink.green, pink.blue);// => 255 105 180Constructor
Section titled “Constructor”Constructor
Section titled “Constructor”new Color(
red,green,blue,alpha?):Color
Creates a new Color from individual RGBA channel values.
Parameters
Section titled “Parameters”| Parameter | Type | Default value | Description |
|---|---|---|---|
|
|
|
|
Integer 0–255 representing the red channel, like |
|
|
|
|
Integer 0–255 representing the green channel, like |
|
|
|
|
Integer 0–255 representing the blue channel, like |
|
|
|
|
Integer 0–255 representing opacity. Defaults to |
Returns
Section titled “Returns”Color
Example
Section titled “Example”// Construct a solid red Colorlet 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);// => 128Properties
Section titled “Properties”
readonlyalpha:number=255
Integer 0–255 representing opacity. Defaults to 255 (fully opaque).
readonlyblue:number
Integer 0–255 representing the blue channel, like 71.
readonlygreen:number
Integer 0–255 representing the green channel, like 99.
readonlyred:number
Integer 0–255 representing the red channel, like 255.
Methods
Section titled “Methods”toHex()
Section titled “toHex()”toHex():
string
Converts this color to an 8-digit hex string. Always returns the full
#RRGGBBAA format, uppercased.
Returns
Section titled “Returns”string
The hex representation, like "#FF6347FF".
Example
Section titled “Example”let c = new SketchUpApi.Color(255, 99, 71, 255);console.log(c.toHex()); // => "#FF6347FF"toNumber()
Section titled “toNumber()”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.
Returns
Section titled “Returns”number
Example
Section titled “Example”let red = new SketchUpApi.Color(255, 0, 0, 255);console.log(red.toNumber()); // => 4278190335toString()
Section titled “toString()”toString():
string
Returns
Section titled “Returns”string
fromHex()
Section titled “fromHex()”
staticfromHex(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 #.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
|
|
|
A hex color string, like |
Returns
Section titled “Returns”Color
A new Color instance.
Example
Section titled “Example”let red = SketchUpApi.Color.fromHex('#FF0000');let semi = SketchUpApi.Color.fromHex('#FF000080');console.log(semi.alpha); // => 128Throws
Section titled “Throws”Error on invalid input.