Skip to content

Instantly share code, notes, and snippets.

@DylanModesitt
Last active January 4, 2018 08:24
Show Gist options
  • Save DylanModesitt/7202d7984f9da43ba14a3526318e099a to your computer and use it in GitHub Desktop.
Save DylanModesitt/7202d7984f9da43ba14a3526318e099a to your computer and use it in GitHub Desktop.
Generate an aesthetically pleasing random color
// generate a bright (v = 0.98), mediumly saturated (s = 0.5) random color
var h=Math.random(),s=0.5,v=0.98,
golden_ratio_conjugate=0.6180339887,
f,p,q,t,r,g,b;
// subsequent calls to rand() follow a more uniform distribution with fibonacci hashing
// see http://cse.iitkgp.ac.in/~pb/algo1-pb-101111.pdf
h += golden_ratio_conjugate;
h %= 1;
// convert from h,s,v to r,g,b
i = Math.floor(h*6);
f = h*6-i;
p = v*(1-s);
q = v*(1-f*s);
t = v*(1-(1-f)*s);
switch (i%6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
r = Math.round(255*r);
g = Math.round(255*g);
b = Math.round(255*b);
var hex = ((1<<24)+(r<<16)+(g<<8)+b).toString(16).substr(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment