Skip to content

Instantly share code, notes, and snippets.

@bendc
Last active February 7, 2024 03:19
Show Gist options
  • Save bendc/76c48ce53299e6078a76 to your computer and use it in GitHub Desktop.
Save bendc/76c48ce53299e6078a76 to your computer and use it in GitHub Desktop.
Generate nice random colors
const randomColor = (() => {
"use strict";
const randomInt = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
return () => {
var h = randomInt(0, 360);
var s = randomInt(42, 98);
var l = randomInt(40, 90);
return `hsl(${h},${s}%,${l}%)`;
};
})();
@ianthedev
Copy link

Thanks for the snippet. Is there a version for directly generating the hex format instead of the hsl format? I need to apply the generated colors on <input type="color">, which only accepts the hex format.

I can probably add a "hsl to hex" function into the snippet, but that would make the snippet unnecessarily longer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment