Skip to content

Instantly share code, notes, and snippets.

@tamimibrahim
Created April 26, 2014 03:16
Show Gist options
  • Save tamimibrahim/11310863 to your computer and use it in GitHub Desktop.
Save tamimibrahim/11310863 to your computer and use it in GitHub Desktop.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
var colortest = $("#colortest");
for (i = 1; i <= 300; i++) {
var c = genColor();
var newhtml = "<span style='font-family:\"Courier\",monospace;width:50px;height:30px;background-color:#" + c + "'>#" + c + "</span>";
colortest.html(colortest.html() + newhtml);
if (i % 10 === 0) {
colortest.html(colortest.html() + "<br/>");
}
}
});
function genColor() {
// take 3 random values, presumably this will be similar to MD5 bytes
var r = Math.floor(Math.random() * 255);
var g = Math.floor(Math.random() * 255);
var b = Math.floor(Math.random() * 255);
// floor again
r = Math.floor(r);
g = Math.floor(g);
b = Math.floor(b);
if (r < 50 && g < 50 && b < 50) r += 50;
if (r > 150 && g > 150 && b > 150) r -= 100;
if (Math.abs(r - g) < 50 && Math.abs(r - b) < 50 && Math.abs(g - b) < 50) {
if (r > 50) r -= 50;
if (g > 50) g -= 50;
if (b < 200) b += 50;
}
var rstr = r.toString(16);
var gstr = g.toString(16);
var bstr = b.toString(16);
// pad 0's -- probably a better way, but this was easy enough.
if (rstr.length === 1) {
rstr = "0" + rstr;
}
if (gstr.length === 1) {
gstr = "0" + gstr;
}
if (bstr.length === 1) {
bstr = "0" + bstr;
}
return rstr + gstr + bstr;
}
</script>
</head>
<body style="margin-left:auto;margin-right:auto">
<div id="colortest" style="width:800px"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment