
Color Array Blend
Downloads: 45
Download Description
Blends Colors from an Array of colors over the set duration
//
// ColorArrayBlend.ts
// Blends through colors in Array over duration set
// Written by @Fusedthreed – https://FusedThreeD.com
//
@customNode()
export class MultiColorBlendOverTime extends BasicScriptNode {
@input()
colors: APJS.Color[];
@input()
duration: number;
@input()
reset: boolean;
@output()
color: APJS.Color;
private startTime: number = -1;
private lastReset: boolean = false;
private finished: boolean = false;
execute() {
const currentTime = Date.now() / 1000;
const resetNow = this.reset === true && this.lastReset === false;
this.lastReset = this.reset === true;
const colorList = this.colors ?? [];
if (resetNow || this.startTime < 0) {
this.startTime = currentTime;
this.finished = false;
}
if (this.finished || colorList.length < 2) return;
const totalDuration = Math.max(this.duration ?? 1, 0.0001);
const elapsed = currentTime - this.startTime;
const segmentCount = colorList.length - 1;
const segmentDuration = totalDuration / segmentCount;
if (elapsed >= totalDuration) {
this.color = colorList[colorList.length - 1];
this.finished = true;
return;
}
const totalT = elapsed / totalDuration;
const segmentIndex = Math.floor(totalT * segmentCount);
const segmentT = (elapsed - (segmentIndex * segmentDuration)) / segmentDuration;
const colorA = colorList[segmentIndex];
const colorB = colorList[segmentIndex + 1];
const r = colorA.r + (colorB.r - colorA.r) * segmentT;
const g = colorA.g + (colorB.g - colorA.g) * segmentT;
const b = colorA.b + (colorB.b - colorA.b) * segmentT;
const a = colorA.a + (colorB.a - colorA.a) * segmentT;
this.color = new APJS.Color(r, g, b, a);
}
}