diff options
author | fiaxh <git@mx.ax.lt> | 2017-08-14 13:48:43 +0200 |
---|---|---|
committer | fiaxh <git@mx.ax.lt> | 2017-08-14 22:38:17 +0200 |
commit | 3ddc53e683b9cdacff7184f3aa805b431edebcdb (patch) | |
tree | 5d9a3ed994ae9193789c7115c294aa9866930074 /plugins/openpgp/src/util.vala | |
parent | b0264b3e0034ddd9ea76d0e13bb4c3c709ba3b3e (diff) | |
download | dino-3ddc53e683b9cdacff7184f3aa805b431edebcdb.tar.gz dino-3ddc53e683b9cdacff7184f3aa805b431edebcdb.zip |
openpgp: contact details provider (fingerprint), colored fingerprints, fix shown availability
Diffstat (limited to 'plugins/openpgp/src/util.vala')
-rw-r--r-- | plugins/openpgp/src/util.vala | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/plugins/openpgp/src/util.vala b/plugins/openpgp/src/util.vala new file mode 100644 index 00000000..7c42b578 --- /dev/null +++ b/plugins/openpgp/src/util.vala @@ -0,0 +1,50 @@ +using Gtk; + +using Dino.Entities; + +namespace Dino.Plugins.OpenPgp { + +/* Adapted from OpenKeychain */ +public static string markup_colorize_id(string s, bool is_fingerprint) { + string markup = is_fingerprint ? "" : "0x"; + for (int i = 0; i < s.length; i += 4) { + string four_chars = s.substring(i, 4).down(); + + int raw = (int) four_chars.to_long(null, 16); + uint8[] bytes = {(uint8) ((raw >> 8) & 0xff - 128), (uint8) (raw & 0xff - 128)}; + + Checksum checksum = new Checksum(ChecksumType.SHA1); + checksum.update(bytes, bytes.length); + uint8[] digest = new uint8[20]; + size_t len = 20; + checksum.get_digest(digest, ref len); + + uint8 r = digest[0]; + uint8 g = digest[1]; + uint8 b = digest[2]; + + if (r == 0 && g == 0 && b == 0) r = g = b = 1; + + double brightness = 0.2126 * r + 0.7152 * g + 0.0722 * b; + + if (brightness < 80) { + double factor = 80.0 / brightness; + r = uint8.min(255, (uint8) (r * factor)); + g = uint8.min(255, (uint8) (g * factor)); + b = uint8.min(255, (uint8) (b * factor)); + + } else if (brightness > 180) { + double factor = 180.0 / brightness; + r = (uint8) (r * factor); + g = (uint8) (g * factor); + b = (uint8) (b * factor); + } + + if (i == 4 * 5) markup += "\n"; + markup += @"<span foreground=\"$("#%02x%02x%02x".printf(r, g, b))\">$four_chars</span>"; + if (is_fingerprint) markup += " "; + } + return "<span font_family='monospace' font='8'>" + markup + "</span>"; +} + +} |