Nice JavaScript code snippet

Arcus Global

Arcus Global
October 3, 2011

Had the need to send a set of hex colour codes to a method which used them to create a chart using Google charts. The initial list was too short so that some of the colours ended up being repeated and the data became difficult to read. I pottered around the code base and “borrowed” some functionality from the boss. The following code is what I used to test it. By all means copy and paste it into a blank file and give it a try. Works a treat!

 <!DOCTYPE HTML>
 <html>
  <head>
   <title>Palette Playing</title>
   <script type="text/javascript">
    var palette = "000000,0000FF,00FFFF,00FF00,FFFFFF,FF0000,FFFF00,FF00FF";
    document.write(palette.split(',').splice(0, 4).join(','));
   </script>
  </head>
  <body>
  </body>
 </html>

This will produce “”000000,0000FF,00FFFF,00FF00”.