Skip to content

Instantly share code, notes, and snippets.

@emrahgunduz
Created September 17, 2016 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emrahgunduz/c2850ac9083af2ad6f990db223094f0d to your computer and use it in GitHub Desktop.
Save emrahgunduz/c2850ac9083af2ad6f990db223094f0d to your computer and use it in GitHub Desktop.
Another rainbor generator, a little more complex
function generateRainbowColors(count, createDiv, frequency1, frequency2, frequency3, phase1, phase2, phase3, center, width) {
var colors = [];
if(!count) count = 50;
if(!createDiv) createDiv = false;
if(!center) center = 128;
if(!width) width = 127;
if(!frequency1) frequency1 = .3;
if(!frequency2) frequency2 = .3;
if(!frequency3) frequency3 = .3;
if(!phase1) phase1 = 0;
if(!phase2) phase2 = 2;
if(!phase3) phase3 = 4;
var ww = window.innerWidth;
var w = Math.ceil(ww / 30);
var x = 0;
var y = 0;
function byte2Hex(n) {
var nybHexString = "0123456789ABCDEF";
return String(nybHexString.substr((n >> 4) & 0x0F,1)) + nybHexString.substr(n & 0x0F,1);
}
function RGB2Color(r,g,b) {
return '#' + byte2Hex(r) + byte2Hex(g) + byte2Hex(b);
}
var red, green, blue, color;
for (var i = 0; i < count; i++){
red = Math.sin(frequency1 * i + phase1) * width + center;
green = Math.sin(frequency2 * i + phase2) * width + center;
blue = Math.sin(frequency3 * i + phase3) * width + center;
color = RGB2Color(red, green, blue);
colors.push(color);
if(createDiv) {
var div = document.createElement("div");
div.style.position = "absolute";
div.style.left = x + "px";
div.style.top = y + "px";
div.style.width = w + "px";
div.style.height = w + "px";
div.style.backgroundColor = color;
document.body.appendChild(div);
x = x + w;
if(x > ww){
x = 0;
y = y + w;
div.style.left = x + "px";
div.style.top = y + "px";
}
}
}
return colors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment