Skip to content

Instantly share code, notes, and snippets.

@NinoScript
Last active November 28, 2021 15:57
Show Gist options
  • Save NinoScript/4812ad503a4098c12ae81ce6daecf1a1 to your computer and use it in GitHub Desktop.
Save NinoScript/4812ad503a4098c12ae81ce6daecf1a1 to your computer and use it in GitHub Desktop.
You can use this generator to get nicely distributed color with the same saturation and lightness.
// TIL that a `function *` in JavaScript makes a generator.
// Generators "yield" values and their execution is paused until next() is called.
function * randomColorGenerator(initialHue, saturation, lightness) {
const phi = 0.618033988749895;
let hue = initialHue;
while(true) {
yield `hsl(${Math.round(hue)}, ${saturation}, ${lightness})`;
hue = (hue + phi * 360) % 360;
}
}
let colorGenerator = randomColorGenerator(Math.random()*360, '80%', '50%');
console.log(generator.next().value);
console.log(generator.next().value);
console.log(generator.next().value);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment