MD5 Flag Generator

This trick was inspired by Brian Suda who I saw speak at Whisky Web.

  1. Take a string, any string.
  2. Hash it using MD5.
  3. Substring the hash to get a 6-digit hex code.
  4. Take a look at what colour that hex code represents.

A really simple, really cool way of generating seemingly random colours, that can be used to represent things.

I had a play with this idea and used it to generate a simple flag for any input string:

<?php

$thing = $_GET['thing'];

$md5 = md5($thing);

$col1 = substr($md5, 0, 6);
$col2 = substr($md5, 6, 6);
$col3 = substr($md5, 12, 6);

echo
    "<div style='float:left;width:100px;height:200px;background:#{$col1}'></div>\n" .
    "<div style='float:left;width:100px;height:200px;background:#{$col2}'></div>\n" 
    "<div style='float:left;width:100px;height:200px;background:#{$col3}'></div>";

This takes an input string via the GET method variable ‘thing’ and turns it in to a 3-stripe flag. Examples:

” (empty string):

‘ben’:

‘bennuttall’:

‘bennuttall.com’:

See this on github: github.com/bennuttall/MD5-Flag-Generator/

Check out Brian’s book Designing With Data

Also have a play with this – I put the code up on my site here

ASCII Bar Charts for Quick & Easy Visualisation

So you have some data. Let’s say it’s a record of the number of instances of some things. Let’s say it’s the number of movies you own, grouped by the year they were released.

Let’s say you have those data in the form of a dictionary in Python, like so:

years = {2000: 2, 2001: 9, 2002: 10, 2003: 9, 2004: 14, 2005: 11, 2006: 8, 2007: 10, 2008: 14, 2009: 19, 2010: 16, 2011: 17}

The following loop will print out an ASCII bar chart for a quick & easy visualisation of these data:

Which looks like this:

Note I used the ‘pipe’ character in this example. First I used ‘o’, which worked well, but I tried a few others ('O','x','X','*','@',':','/','#','[]','+','-','=','_',':)',…) and liked this the most.

That’s the end of what I wanted the blog post to show, but I may as well throw in how I got my data in this case. I have movies saved in a folder, and by convention I name them with the year in brackets at the end so I used glob to loop through the files in this folder, extract the year, and increment the counter in my years dictionary. I have another blog post in draft about using glob to edit filenames in batch. Coming soon.