Created
June 14, 2016 06:29
-
-
Save Inclushe/35a3fae1ffc0b0d8a14f95a91f8ea0ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function rand(min,max,round) { | |
if(round === false){ | |
return (Math.random() * (max-min)) + min; | |
} | |
else { | |
return Math.round((Math.random() * (max-min)) + min); | |
} | |
} | |
function createNoiseFromColors(w, h){ | |
var canvasElement = document.createElement("canvas"); | |
canvasElement.id = "canvas" + rand(100000,999999); | |
document.body.appendChild(canvasElement); | |
var c = document.getElementById(canvasElement.id); | |
var ctx = c.getContext("2d"); | |
c.width = w; | |
c.height = h; | |
var colors = []; | |
for (var argIndex = 2; argIndex < arguments.length; argIndex++){ | |
colors.push(arguments[argIndex]); | |
} | |
for (var colorIndex = 0; colorIndex < colors.length; colorIndex++){ | |
switch (colors[colorIndex]){ | |
case "black": | |
colors[colorIndex] = [0,0,0]; | |
break; | |
case "white": | |
colors[colorIndex] = [255,255,255]; | |
break; | |
case "red": | |
colors[colorIndex] = [255,0,0]; | |
break; | |
case "green": | |
colors[colorIndex] = [0,255,0]; | |
break; | |
case "blue": | |
colors[colorIndex] = [0,0,255]; | |
break; | |
case "yellow": | |
colors[colorIndex] = [255,255,0]; | |
break; | |
case "magenta": | |
case "purple": | |
case "pink": | |
colors[colorIndex] = [255,0,255]; | |
break; | |
case "cyan": | |
colors[colorIndex] = [0,255,255]; | |
break; | |
default: | |
colors[colorIndex] = colors[colorIndex].replace(/[^a-fA-F0-9]/,""); | |
var hexValues = colors[colorIndex].match(/.{2}/g); | |
var decValues = []; | |
for (var hexIndex = 0; hexIndex < hexValues.length; hexIndex++){ | |
decValues.push(parseInt(hexValues[hexIndex],16)); | |
} | |
colors[colorIndex] = decValues; | |
} | |
} | |
ctx.fillStyle = "white"; | |
ctx.fillRect(0,0,c.width,c.height); | |
var imagething = ctx.getImageData(0,0,c.width,c.height); | |
var data = imagething.data; | |
for (var i = 0; i < (data.length * 4); i++){ | |
var randomIndex = rand(0,colors.length-1); | |
data[(i*4)+0] = colors[randomIndex][0]; | |
data[(i*4)+1] = colors[randomIndex][1]; | |
data[(i*4)+2] = colors[randomIndex][2]; | |
} | |
ctx.putImageData(imagething, 0, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment