aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeterminant <tederminant@gmail.com>2020-05-22 11:05:08 -0400
committerDeterminant <tederminant@gmail.com>2020-05-22 11:05:08 -0400
commitd77d01d5c24a891aa12dde0c61a187ddf36ac9b9 (patch)
tree3f81cd401596a31935b98bc69d4b5d0b8e291efe
parent443f0073270c57c12654bc26b6eb7204bda379cb (diff)
support python2
-rwxr-xr-xname2color.py7
1 files changed, 6 insertions, 1 deletions
diff --git a/name2color.py b/name2color.py
index e68149f..7c1d11b 100755
--- a/name2color.py
+++ b/name2color.py
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
import sys
+import struct
import hashlib
# add your favorite color code (256) here as candidates
colors = [
@@ -8,4 +9,8 @@ colors = [
160, 161, 167, 137, 131
]
h = hashlib.sha1(sys.argv[1].encode('utf-8')).digest()
-print("colour{}".format(colors[h[0] % len(colors)]))
+if sys.version_info.major == 3:
+ hn = int.from_bytes(h, byteorder='big')
+else:
+ (hn,) = struct.unpack('>Q12x', h)
+print("colour{}".format(colors[hn % len(colors)]))