Files
spore-ledlab/presets/fade-preset.js
2025-10-12 10:35:56 +02:00

69 lines
2.1 KiB
JavaScript

// Fade preset for LEDLab
const BasePreset = require('./base-preset');
const { createFrame, frameToPayload } = require('./frame-utils');
class FadePreset extends BasePreset {
constructor(width = 16, height = 16) {
super(width, height);
this.tick = 0;
this.defaultParameters = {
speed: 0.5, // cycles per second
brightness: 1.0,
color1: '00ff00', // Green
color2: '0000ff', // Blue
};
}
hexToRgb(hex) {
const r = parseInt(hex.slice(0, 2), 16);
const g = parseInt(hex.slice(2, 4), 16);
const b = parseInt(hex.slice(4, 6), 16);
return { r, g, b };
}
renderFrame() {
const frame = createFrame(this.width, this.height);
const timeSeconds = (this.tick * 0.016); // Assume 60 FPS
const phase = timeSeconds * this.getParameter('speed') * Math.PI * 2;
const blend = (Math.sin(phase) + 1) * 0.5; // 0..1
const brightness = this.getParameter('brightness') || 1.0;
const color1 = this.hexToRgb(this.getParameter('color1') || '00ff00');
const color2 = this.hexToRgb(this.getParameter('color2') || '0000ff');
const r = Math.round((color1.r * (1 - blend) + color2.r * blend) * brightness);
const g = Math.round((color1.g * (1 - blend) + color2.g * blend) * brightness);
const b = Math.round((color1.b * (1 - blend) + color2.b * blend) * brightness);
const rHex = r.toString(16).padStart(2, '0');
const gHex = g.toString(16).padStart(2, '0');
const bHex = b.toString(16).padStart(2, '0');
for (let i = 0; i < frame.length; i++) {
frame[i] = rHex + gHex + bHex;
}
this.tick++;
return frame;
}
getMetadata() {
return {
name: 'Fade',
description: 'Smooth fade between two colors',
parameters: {
speed: { type: 'range', min: 0.1, max: 2.0, step: 0.1, default: 0.5 },
brightness: { type: 'range', min: 0.1, max: 1.0, step: 0.1, default: 1.0 },
color1: { type: 'color', default: '00ff00' },
color2: { type: 'color', default: '0000ff' },
},
width: this.width,
height: this.height,
};
}
}
module.exports = FadePreset;