Skip to content

Instantly share code, notes, and snippets.

@manuelvanrijn
Created April 15, 2011 09:02
Show Gist options
  • Save manuelvanrijn/921430 to your computer and use it in GitHub Desktop.
Save manuelvanrijn/921430 to your computer and use it in GitHub Desktop.
generate random greenish color
function randomGreenishColor(i, total) {
var g = randomFromTo(128, 255);
var r = randomFromTo(0, g-50);
var b = randomFromTo(0, g-50);
return rgbToHex(r,g,b);
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function randomFromTo(from, to) {
return Math.floor(Math.random() * (to - from + 1) + from);
}
/* HTML */
var total = 255;
for(var i=0; i< total; i++) {
var color = randomGreenishColor(i, total);
// TODO: do something with it.
// ex: $("body").append("<div style='background-color: "+color+"'></div>");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment