Skip to content

Instantly share code, notes, and snippets.

@mozz100
Last active December 26, 2015 10:09
Show Gist options
  • Save mozz100/7134567 to your computer and use it in GitHub Desktop.
Save mozz100/7134567 to your computer and use it in GitHub Desktop.
Mozzicon: html/python implementation of https://github.com/cupcake/sigil
def mozzicon(md5):
# Inspired by (!) https://github.com/cupcake/sigil
# return a 5 by 5 grid of divs with horizontal plane of symmetry
# (css used to rotate by 90deg)
# md5 is a string like "9eadbe04ba0a832eecbd0a27f563e0a6"
# use first char of hash to pick a color from 7
colors = ('2d4fff', 'feb42c', 'e279ea', '1eb3fd', 'e84d41', '31cb73', '8d45aa', )
fgcolor = "#%s" % colors[ ord(md5[0:2].decode("hex")) % 7 ]
# use 4 chars/2 bytes of hash for 1's and 0's.
# We want each byte as a string like "10010101" (padded to 8 chars)
# and we throw away the last bit, yielding 3 rows of 5 "bits".
# Take characters 3,4,5,6 and decode them to integers
ints = md5[3:7].decode("hex")
# turn each integer into a string of 1's and 0's of length 8
binstrs = map(lambda x: bin(ord(x))[2:].rjust(8,"0"), ints)
# join them together and just take 15 of them
pixels = "".join(binstrs)[0:15]
# symmetry: 1,2,3,2,1
pixels += pixels[5:10]
pixels += pixels[0:5]
html = "".join(map(lambda x: "<div class='pixel%s'></div>" % (x, ), pixels))
html = "<div class='mozzicon' style='background-color: %s;' title='%s'>%s</div>" % (fgcolor, md5, html)
return mark_safe(html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment