Created
March 27, 2016 04:52
-
-
Save benwaffle/35ad7d6c49a6336ea1a8 to your computer and use it in GitHub Desktop.
identicon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void draw_rect (Cairo.Context context, double x, double y, double width, double height, Gdk.RGBA color) { | |
context.set_source_rgba (color.red, color.green, color.blue, color.alpha); | |
context.rectangle (x, y, width, height); | |
context.fill (); | |
} | |
int parse_base16 (string? s) { | |
if (s == null) | |
return 0; | |
int res; | |
s.scanf ("%x", out res); | |
return res; | |
} | |
Gdk.RGBA hsl2rgb (double h, double s, double b) { | |
h *= 6; | |
double hsl[] = { | |
b += s *= b < 0.5 ? b : 1 - b, | |
b - h % 1 * s * 2, | |
b -= s *= 2, | |
b, | |
b + h % 1 * s, | |
b + s | |
}; | |
var rgb = Gdk.RGBA (); | |
rgb.red = hsl[ (int)h % 6 ]; | |
rgb.green = hsl[ ((int)h|16) % 6 ]; | |
rgb.blue = hsl[ ((int)h|8) % 6 ]; | |
rgb.alpha = 1; | |
return rgb; | |
} | |
string do_hash (string? _str) { | |
uint32 hash = 0; | |
if (_str == null) | |
return hash.to_string (); | |
string str = _str + "identicon" + _str; | |
for (int i = 0; i < str.length; ++i) { | |
char chr = str[i]; | |
hash = ((hash << 5) - hash) + (int) chr; | |
} | |
return hash.to_string (); | |
} | |
void main (string[] args) { | |
Gtk.init (ref args); | |
var win = new Gtk.Window (); | |
var box = new Gtk.Box (Gtk.Orientation.VERTICAL, 6); | |
var e = new Gtk.Entry (); | |
var icon = new Gtk.DrawingArea (); | |
e.changed.connect (() => { | |
icon.queue_draw (); | |
}); | |
icon.draw.connect (cr => { | |
var hash = do_hash (e.text); | |
double margin = 0.08; | |
int size = 256; | |
var baseMargin = Math.floor(size * margin); | |
var cell = Math.floor((size - (baseMargin * 2)) / 5); | |
margin = Math.floor((size - cell * 5) / 2); | |
// Background color | |
var background = Gdk.RGBA () { | |
red = 240.0/255, | |
green = 240.0/255, | |
blue = 240.0/255, | |
alpha = 1 | |
}; | |
// Foreground color | |
var foreground = hsl2rgb ((double)parse_base16 (hash.substring (-7)) / 0xfffffff, 0.5, 0.7); | |
// The first 15 characters of the hash control the pixels (even/odd) | |
// they are drawn down the middle first, then mirrored outwards | |
for (var i = 0; i < 15; i++) { | |
var color = (parse_base16 ("%c".printf(hash[i])) % 2) != 0 ? background : foreground; | |
if (i < 5) { | |
draw_rect (cr, 2 * cell + margin, i * cell + margin, cell, cell, color); | |
} else if (i < 10) { | |
draw_rect (cr, 1 * cell + margin, (i - 5) * cell + margin, cell, cell, color); | |
draw_rect (cr, 3 * cell + margin, (i - 5) * cell + margin, cell, cell, color); | |
} else if (i < 15) { | |
draw_rect (cr, 0 * cell + margin, (i - 10) * cell + margin, cell, cell, color); | |
draw_rect (cr, 4 * cell + margin, (i - 10) * cell + margin, cell, cell, color); | |
} | |
} | |
return true; | |
}); | |
win.delete_event.connect (() => { | |
Gtk.main_quit (); | |
return true; | |
}); | |
box.pack_start (e, false, false); | |
box.pack_end (icon, true, true); | |
win.add (box); | |
win.set_default_size (400, 300); | |
win.show_all (); | |
Gtk.main (); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment