chadlindstrom.ca

Adventurer, Web Developer, Photographer, Athlete and goofball




JavaScript Hex Functions

Using Hex numbers in JavaScript.

The Code

<style type="text/css"> code { white-space:pre;} #hex-table { border-collapse:collapse; border:thin solid black; width:80%;} #hex-table td { padding:0.2em; text-align:center;} </style> <script type="text/javascript"> function d2h(d) { if(d.toString(16) == 0){ return "00"; } return d.toString(16).toUpperCase(); } function generate_table() { var output = "\n"; for(var i = 0x00; i <= 0xFF; i+=0x33) { for(var j = 0x00; j <= 0xFF; j+=0x33) { output += "<tr>\n"; for(var k = 0x00; k <= 0xFF; k+=0x33) { var hex = "#"+d2h(i)+d2h(j)+d2h(k); output += "\t<td style='background-color:"+hex+";'>"+hex+"</td>\n"; } output += "</tr>\n"; } } $("#hex-table").html(output); } $(document).ready(function(){ generate_table(); $("#markup_code").html( html_entity_encode( $(".markup").html() ) ); }); </script>

The Markup

The Output