Some time ago, GitHub retired the default cat icon and introduced Identicons. While we were working on the nub.is 404 page, Walid replaced the blocks in his GitHub identicon by Nubis blocks (from our cloud icon) and uploaded it to Gravatar.
That (and the fact that we just got a new server by WebFaction, which I might blog about later) inspired me to make Nubis identicons.
They work similar to gravatar, in that you first get a hash for your user name and can then request an icon for your hash. Of course, I had to one-up GitHub, so our identicons are 3D! (This might also be because the blocks are identicons)
You can check out the full source code on GitHub, but here are the highlights. I have two classes, Identicon
and Icon
. Icon
represents an actual image, in Imagemagick. Identicon
represents the identicon.
I chose to use Sinatra to handle the HTTP, which is beautifully succinct:
get '/:hash.png', :provides => :png do |hash|
icon = Identicon::from_hash hash
icon.to_png
end
get '/:hash.coords', :provides => :text do |hash|
icon = Identicon::from_hash hash
icon.to_coord
end
get '/:hash.json', :provides => :json do |hash|
icon = Identicon::from_hash hash
icon.to_a.to_json
end
get '/:name' do |name|
icon = Identicon.new name
icon.to_hash
end
As you can see, HTTP methods map to model methods quite clean-ly.
Practically, our icons probably aren’t great because of collisions. I reduce the SHA-2 (which is quite collision-proof) hash which has 256 bits of data to 64 bits of data (the 64 blocks in the picture). Furthermore, some icons will not be distinguishable from each other because of the way you look at them.
But hey, the pictures are kind of pretty!