UNPKG

@ginden/blinkstick-v2

Version:
38 lines 1.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.morphMany = morphMany; const simple_frame_1 = require("../frame/simple-frame"); const utils_1 = require("../../utils"); const assert_fps_below_100_1 = require("../helpers/assert-fps-below-100"); const tsafe_1 = require("tsafe"); /** * Smooth transition between multiple RGB colors. * @category Animations */ function morphMany(tuples, overMs, steps = (overMs / 33) | 0) { (0, tsafe_1.assert)(tuples.length > 1, 'At least two tuples are required for morphing.'); (0, assert_fps_below_100_1.assertFpsBelow100)(overMs, steps); const segments = tuples.length - 1; const baseSteps = Math.floor(steps / segments); const totalStepsAllocated = baseSteps * segments; // Distribute the remaining frames to the last segment const remainder = steps - totalStepsAllocated; const stepDuration = overMs / steps; return { *[Symbol.iterator]() { for (let i = 0; i < segments; i++) { const from = tuples[i]; const to = tuples[i + 1]; const segmentSteps = i === segments - 1 ? baseSteps + remainder : baseSteps; for (let j = 0; j < segmentSteps; j++) { const ratio = j / (segmentSteps - 1); const r = (0, utils_1.clampRgb)(Math.round(from[0] + (to[0] - from[0]) * ratio)); const g = (0, utils_1.clampRgb)(Math.round(from[1] + (to[1] - from[1]) * ratio)); const b = (0, utils_1.clampRgb)(Math.round(from[2] + (to[2] - from[2]) * ratio)); yield new simple_frame_1.SimpleFrame([r, g, b], stepDuration); } } }, }; } //# sourceMappingURL=morph-many.js.map