Last active
November 28, 2021 15:57
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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