Created
October 16, 2012 11:24
-
-
Save mucar/3898752 to your computer and use it in GitHub Desktop.
Generate Random Color Array
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" | |
"http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | |
<title>Random Color Array</title> | |
</head> | |
<body> | |
<input type="text" id="renkler" value=""/> | |
<script type="text/javascript"> | |
function generateColorArray(){ | |
var colorArray = []; | |
//11*11*11 = 1331 farklı renk oluşturulacak. | |
c = new Array('00', '1A', '33', '4D', '66', '80', | |
'99', 'B3', 'CC', 'E6','FF'); | |
//6*6*6 = 216 adet web safe renkleri oluşturmak | |
//için ise aşağıdaki dizi kullanılmalıdır. | |
//c = new Array('00', '33', '66', '99', 'CC', 'FF'); | |
for (i = 0; i < c.length; i++) { | |
for (j = 0; j < c.length; j++) { | |
for (k = 0; k < c.length; k++) { | |
l = c[i] + c[j] + c[k]; | |
colorArray.push(l); | |
} | |
} | |
} | |
return colorArray; | |
} | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
Array.prototype.removeByIndex = function(index) { | |
this.splice(index, 1); | |
} | |
Array.prototype.pullRandomly = function() { | |
var randomInt = getRandomInt(0, this.length-1); | |
var randomElement = this[randomInt]; | |
this.removeByIndex(randomInt); | |
return randomElement; | |
} | |
var colorArray = generateColorArray(); | |
var randomColorArray = []; | |
//for döngüsüne direkt olarak dizi boyutunu vermek | |
//yerine özellikle değişkene atama yaptık. Çünkü | |
//dizinin boyutu her seferinde 1 azalıyor. | |
var colorArraySize = colorArray.length; | |
for ( var i = 0; i <colorArraySize; i++) { | |
randomColorArray.push(colorArray.pullRandomly()); | |
} | |
var randomColorArraySize = randomColorArray.length; | |
var html = '<table width="50%"><tr>'; | |
for ( var j = 0; j < randomColorArraySize; j++) { | |
var l = randomColorArray[j]; | |
html+='<td bgcolor=#'+l+' onclick='+ | |
'"document.getElementById(\'renkler\').value '+ | |
'+= this.innerText + \', \';">#'+l+'</td>'; | |
if((j+1)%11==0){ | |
html += '</tr>'; | |
} | |
} | |
document.write(html+'</table>'); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment