Skip to content

Instantly share code, notes, and snippets.

@WuerfelDev
Last active May 8, 2019 18:39
Show Gist options
  • Save WuerfelDev/c8e63e08547b6672317568e453f59a5f to your computer and use it in GitHub Desktop.
Save WuerfelDev/c8e63e08547b6672317568e453f59a5f to your computer and use it in GitHub Desktop.
Nice random color generator
var colorCache = Array();
function randomColor() {
const r = () => 200 + Math.floor(56 * Math.random()); //Strong color
var rl = Array();
do {
rl[0] = r();
rl[1] = r() - 50 - Math.floor(206 * Math.random()); // Clearer defined color
rl[2] = r() - 50 - Math.floor(50 * Math.random());
rl.sort(function() {
return 0.5 - Math.random();
});
} while(!validColor(rl));
rl[0] -= 40;
//Negative not handled, if needed bring it to Zero
return rl;
}
function validColor(rl) {
if( ((rl[0]+rl[1]+rl[2])<350)){ //Color brightness
if(colorCache.length != 0){ //Cache not empty
if(colorCache.indexOf(Math.max(...this.colorCache)) == rl.indexOf(Math.max(...rl)) ){ //not same base color
return false;
}
}
this.colorCache = rl;
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment