-
-
Save adarrra/839a930b10f0e3bd12c5ad8a93662e1c to your computer and use it in GitHub Desktop.
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)); | |
} |
Hash functions:
http://www.cse.yorku.ca/~oz/hash.html
http://erlycoder.com/49/javascript-hash-functions-to-convert-string-into-integer-hash-
http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery
http://stackoverflow.com/questions/17083316/hashing-a-string-between-two-integers-with-a-good-distribution-uniform-hash
what is faster: https://jsperf.com/stupid-smart-hash-functions/1
Seeded random generators
http://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript
http://davidbau.com/archives/2010/01/30/random_seeds_coded_hints_and_quintillions.html#more
http://indiegamr.com/generate-repeatable-random-numbers-in-js/
can use this in proj http://chancejs.com/
Color generators
https://github.com/davidmerfield/randomColor
based on golden ratio https://github.com/devongovett/color-generator
idea about golden ratio http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
Test
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
With golden ratio (maybe better to use clojure)