Skip to content

Instantly share code, notes, and snippets.

@slawo-ch
Created December 23, 2019 14:39
Show Gist options
  • Save slawo-ch/23fdbac02eec4bf5d151008f4fb3aaed to your computer and use it in GitHub Desktop.
Save slawo-ch/23fdbac02eec4bf5d151008f4fb3aaed to your computer and use it in GitHub Desktop.
// stores whether we're interpolating colors
// from palette 0 -> 1 (1) or 1 -> 0 (-1)
let prevDirection = 1;
const updatePalette = t => {
const timeScale = 0.0005;
const x = t * timeScale;
// normalized value 0..1 used to interpolate palette colors
const inter = (Math.cos(x) + 1) / 2;
// did we switch direction, and should ergo pick a new palette
// random palette to interpolate towards?
const direction = -Math.sin(x) >= 0 ? 1 : -1;
if (prevDirection != direction) {
prevDirection = direction;
if (direction == -1) {
palettes[0] = makeRandomPalette();
} else {
palettes[1] = makeRandomPalette();
}
}
// create interpolated palette for current frame
for (let i = 0; i < 256; i++) {
palette[i] = interpolate(palettes[0][i], palettes[1][i], inter);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment