Skip to content

Instantly share code, notes, and snippets.

@mucar
Created October 16, 2012 11:24
Show Gist options
  • Select an option

  • Save mucar/3898752 to your computer and use it in GitHub Desktop.

Select an option

Save mucar/3898752 to your computer and use it in GitHub Desktop.

Revisions

  1. mucar revised this gist Oct 16, 2012. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions generate_random_color_array.html
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <!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">
    @@ -11,9 +12,11 @@
    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');
    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.
    //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++) {
    @@ -61,7 +64,9 @@

    for ( var j = 0; j < randomColorArraySize; j++) {
    var l = randomColorArray[j];
    html+='<td bgcolor=#'+l+' onclick="document.getElementById(\'renkler\').value += this.innerText + \', \';">#'+l+'</td>';
    html+='<td bgcolor=#'+l+' onclick='+
    '"document.getElementById(\'renkler\').value '+
    '+= this.innerText + \', \';">#'+l+'</td>';
    if((j+1)%11==0){
    html += '</tr>';
    }
  2. mucar created this gist Oct 16, 2012.
    73 changes: 73 additions & 0 deletions generate_random_color_array.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    <!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>