Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder oldcrazylegs's Avatar
    Join Date
    Feb 2004
    Location
    East Moline Illinois USA
    Posts
    414
    Thanks
    4
    Thanked 5 Times in 4 Posts

    Color Chart Script

    I have been using this counting script for something else.

    But now I want it to count from 0-65535, put those numbers inside small div tags and have those div tags have the background color of the number it is counting.

    It would be nice if the div tags went across the page and wrapped like text and started a new row.

    Maybe counting that far will use too much memory. If so I can put x amount on one page and more on another and so on.

    I'm still kicking myself for not learning JavaScript when I got online years ago. It would have certainly been good to know now.

    Code:
    <script type="text/javascript">
    for(var count=0; count<=65535; count++) 
    {
    document.write('<div style=background-color:&#>');
    document.write(count + '; = </div>');
    document.write('&#' + count + ';');
    }
    </script>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    You will need to turn the decimal values of the loop counter into hex. Something like this:-



    Code:
    <script type="text/javascript">
    
    for (var a = 0; a<=5; a++) {  // change 5 to 255 for final version
    for (var b = 0; b<=5; b++) {  // change 5 to 255 for final version
    for (var c = 0; c<=5; c++) {   // change 5 to 255 for final version
    
    var hexa =  a.toString(16).toUpperCase();  // hex
    var hexb =  b.toString(16).toUpperCase();  // hex
    var hexc =  c.toString(16).toUpperCase();  // hex
    
    if (hexa.length<2) {hexa = "0" + hexa}
    if (hexb.length<2) {hexb = "0" + hexb}
    if (hexc.length<2) {hexc = "0" + hexc}
    
    var f = hexa+hexb+hexc;
    document.write(f + "<br>")
    
    }
    }
    }
    
    </script>
    But I think you will find that 65535 <div>s choke the browser.

    Quizmaster: Who wrote the William Tell Overture?
    Contestant: Was it William Tell?
    Last edited by Philip M; 04-11-2013 at 10:20 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder oldcrazylegs's Avatar
    Join Date
    Feb 2004
    Location
    East Moline Illinois USA
    Posts
    414
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •