Re: Factor

Factor: the language, the theory, and the practice.

Robohash

Friday, July 29, 2011

#hash #math

A few days ago, I read about Robohash, a website for creating unique images (of robots) from any text. The author was thinking of using it for icons on a forum, but there are probably other use-cases for this. For example, as a variation of the color password hash to help users avoid typos in their passwords or, perhaps, as a visual representation of a user-submitted “secret code”.

USING: images.http kernel sequences urls urls.encoding ;

First, we need to create URLs of the form https://robohash.org/YOUR_TEXT, as instructed:

: robohash-url ( str -- url )
    url-encode "https://robohash.org/" prepend >url ;

Next, we would like to support the different “image sets” that Robohash supports.

: (robohash) ( str type -- image )
    [ robohash-url ] [ "set" set-query-param ] bi*
    load-http-image ;

Using this, we can create image loaders for each set (currently three sets: “set1”, “set2”, and “set3”):

: robohash1 ( str -- image ) "set1" (robohash) ;

: robohash2 ( str -- image ) "set2" (robohash) ;

: robohash3 ( str -- image ) "set3" (robohash) ;

You can try it out and see that it works:

Robohash also supports custom backgrounds, changing image sizes, and varying image formats (e.g., JPG or BMP). Adding support for that is an exercise to the reader.

The code for this is on my GitHub.