Skip to content

Instantly share code, notes, and snippets.

@adarrra
Last active May 4, 2017 09:42
Show Gist options
  • Save adarrra/839a930b10f0e3bd12c5ad8a93662e1c to your computer and use it in GitHub Desktop.
Save adarrra/839a930b10f0e3bd12c5ad8a93662e1c to your computer and use it in GitHub Desktop.
Random color generator
function getColor(someString) {
// simple stupid hash func, beware of Number.MAX_VALUE => Infinity => Nan :) alsow its slow
const hash = parseInt(Array.from(someString, char => char.charCodeAt(0)).join(''));
const hue = hash%360;
const saturation = seededRand(hue, 13, 28);
const lightness = seededRand(hue, 65, 72);
return `hsla(${hue}, ${saturation}%, ${lightness}%, 1)`;
}
function seededRand(seed, min, max) {
const rnd = parseFloat('0.'+Math.sin(seed).toString().substr(8));
return Math.floor(min + rnd * (max - min));
}
@adarrra
Copy link
Author

adarrra commented Mar 15, 2017

Further reading

davidmerfield/randomColor#50
and from there
http://stackoverflow.com/questions/470690/how-to-automatically-generate-n-distinct-colors
"It boils down to: RGB (even HSV) does not suitably model human color-perception; for smaller sets, it's best to use man-made (=evaluated) fixed color pallet and pick from it; for arbitrary number, use some specialized algorithm."
long read https://eleanormaclure.files.wordpress.com/2011/03/colour-coding.pdf

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