Color
APJS Script API reference for the Color class.
| Type | Name | Interface Description |
|---|---|---|
| Variables | a: number | • Function: Color's channel A |
| Variables | b: number | • Function: Color's channel B |
| Variables | g: number | • Function: Color's channel G |
| Variables | r: number | • Function: Color's channel R |
| Functions | constructor(r?: number, g?: number, b?: number, a?: number) | Parameters • • • • |
| Functions | clone(): Color | • Function: Creates and returns a copy of the current color instance. Returns A new Color object with the same properties as the original. |
| Functions | equals(v: Color): boolean | • Function: Compares the current color with another color for equality. Parameters • Returns A boolean indicating whether the two colors are equal. |
| Functions | toString(): 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 Functions | equals(v0: Color, v1: Color): boolean | • Function: Compares two colors for equality. Parameters • • 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 {}
}