Skip to main content

Color

APJS Script API reference for the Color class.

TypeNameInterface Description
Variablesa: number

Function: Color's channel A

Variablesb: number

Function: Color's channel B

Variablesg: number

Function: Color's channel G

Variablesr: number

Function: Color's channel R

Functionsconstructor(r?: number, g?: number, b?: number, a?: number)

Parameters

r: - The red component of the color, or an existing Color object. Defaults to 0 if not provided.

g: - The green component of the color. Defaults to 0 if not provided.

b: - The blue component of the color. Defaults to 0 if not provided.

a: - The alpha (opacity) component of the color. Defaults to 0 if not provided.

Functionsclone(): Color

Function: Creates and returns a copy of the current color instance.

Returns A new Color object with the same properties as the original.

Functionsequals(v: Color): boolean

Function: Compares the current color with another color for equality.

Parameters

v: - The color to compare against.

Returns A boolean indicating whether the two colors are equal.

FunctionstoString(): string

Function: Converts the color to a string representation.

Returns A string representation of the color in the format "Color(R: r, G: g, B: b, A: a)" where r, g, b, and a are fixed to 5 decimal places.

Static Functionsequals(v0: Color, v1: Color): boolean

Function: Compares two colors for equality.

Parameters

v0: - The first color to compare.

v1: - The second color to compare.

Returns True if the colors are equal, false otherwise.

Examples

constructor(r?: number, g?: number, b?: number, a?: number)

let obj = new APJS.Color();

Use Case

Example 1 — Tap screen to change object color (cycles through preset colors)

@component()
export class TapChangeColor extends APJS.BasicScriptComponent {
private touchCallback: (event: APJS.IEvent) => void;
private colorIndex: number = 0;
private colors: APJS.Color[] = [
new APJS.Color(1, 0, 0, 1),
new APJS.Color(0, 1, 0, 1),
new APJS.Color(0, 0, 1, 1),
new APJS.Color(1, 1, 0, 1),
];

onStart(): void {
this.touchCallback = (event: APJS.IEvent) => {
const touchInfo = event.args[0] as APJS.TouchData;
if (touchInfo.phase === APJS.TouchPhase.Began) {
this.colorIndex = (this.colorIndex + 1) % this.colors.length;
const image = this.getSceneObject().getComponent("Image") as APJS.Image;
if (image) {
image.color = this.colors[this.colorIndex];
}
}
};
APJS.EventManager.getGlobalEmitter().on(APJS.EventType.Touch, this.touchCallback, this);
}

onDestroy(): void {
if (this.touchCallback) {
APJS.EventManager.getGlobalEmitter().off(APJS.EventType.Touch, this.touchCallback, this);
}
}
}

Example 2 — Change a 3D object's material color at runtime via MeshRenderer.mainMaterial

@component()
export class MaterialColorChange extends APJS.BasicScriptComponent {
@serializeProperty
private targetColor: APJS.Color = new APJS.Color(1, 0, 0, 1);

onStart(): void {
this.applyColor(this.targetColor);
}

private applyColor(color: APJS.Color): void {
const renderer = this.getSceneObject().getComponent("MeshRenderer") as APJS.MeshRenderer;
// Use mainMaterial for single material access
// Use setVector (NOT setVector4 — it doesn't exist)
if (renderer && renderer.mainMaterial) {
renderer.mainMaterial.setVector("_AlbedoColor", new APJS.Vector4f(color.r, color.g, color.b, color.a));
}
}

onDestroy(): void {}
}
Copyright © 2026 TikTok. All rights reserved.
About TikTokHelp CenterCareersContactLegalTerms of ServicePrivacy PolicyCookies