var DistinctColorManager = function (options) { var saturation = "100%", lightness = "60%", next_hue = 0, degrees = 0.6, pctRegex = /^(100|([1-9]?[0-9]{1}))%$/, degressRegex = /^d+$/, me = this; options = options || {}; lightness = options.lightness || lightness; saturation = options.saturation || lightness; function validatePercentage(val) { return pctRegex.test(val); } function get_hue(idx) { /* Here we use 31 bit numbers because JavaScript doesn't have a 32 bit unsigned type, and so the conversion to float would produce a negative value. */ var bitcount = 31; /* Reverse the bits of idx into ridx */ var ridx = 0, i = 0; for (i = 0; i < bitcount; i++) { ridx = (ridx << 1) | (idx & 1); idx >>>= 1; } /* Divide by 2**bitcount */ var hue = ridx / Math.pow(2, bitcount); /* Start at .6 (216 degrees) */ return (hue + degrees) % 1; } return { resetHueIndex: function () { next_hue = 0; }, setHueIndex: function (val) { next_hue = typeof val === 'undefined' || parseInt(val) === NaN ? next_hue : parseInt(val, 10); }, setSaturation: function (val) { if (pctRegex.test(val)) { saturation = val; } }, setDegrees: function (val) { if (degressRegex.test(val)) { degrees = (parseInt(val, 10) / 350); } }, setLightness: function (val) { if (pctRegex.test(val)) { lightness = val; } }, getCurrentHueIndex: function () { return next_hue; }, getHSL: function () { var hue = Math.round(get_hue(next_hue++) * 360); hsl = 'hsl(' + hue + ',' + saturation + ',' + lightness + ')'; return hsl; } }; }; var oDCM = DistinctColorManager(); var lis = []; for (var i = 0; i < 100; i++) { var el = document.createElement('li'); lis.push(el); container.appendChild(el); } setInterval(function () { oDCM.setSaturation((~~ (Math.random() * 100)) + '%'); //oDCM.setLightness((~~ (Math.random() * 100)) + '%'); oDCM.setLightness('50%'); oDCM.setDegrees((~~ (Math.random() * 360))); for (i = 0, len = lis.length; i < len; i++) { console.log() lis[i].style.backgroundColor = oDCM.getHSL(); } }, 1000)
<ul id='container'></ul>
ul { list-style:none; } li { width:700px; height: 20px; transition: background-color 1s linear; -webkit-transition:backgroundColor 1s linear; }