Last active
May 4, 2017 09:42
-
-
Save adarrra/839a930b10f0e3bd12c5ad8a93662e1c to your computer and use it in GitHub Desktop.
Random color generator
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
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)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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