Last active
September 3, 2021 00:48
-
-
Save pinealan/8a31ded72763303ab7a417611f18cda7 to your computer and use it in GitHub Desktop.
Compressed encoding for web safe color.
This file contains 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
import base64 | |
import itertools | |
import zlib | |
# look up table for web safe color rgb hex | |
allowed_hex = ['0', '3', '6', '9', 'c', 'f'] | |
code_to_hex = {idx: ''.join(s) | |
for idx, s in enumerate(itertools.product(allowed_hex, repeat=3))} | |
hex_to_code = {v: k for k, v in code_to_hex.items()} | |
def chunk(s): | |
return [s[i:i+3] for i in range(0, len(s), 3)] | |
def encode(s: str): | |
return base64.b64encode(zlib.compress(bytes([hex_to_code[x] for x in s]))) | |
def decode(s: str) -> str: | |
return ''.join(code_to_hex[b] for b in zlib.decompress(base64.b64decode(s))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
String representation for donda image
Corresponding length: