Created
April 27, 2023 17:23
-
-
Save gimre-xymcity/18b710d5aaa6ed728f1e230dc6df8df2 to your computer and use it in GitHub Desktop.
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 csv | |
import datetime | |
from pathlib import Path | |
def tohex(entry): | |
return f'#{entry[0]:02x}{entry[1]:02x}{entry[2]:02x}' | |
def short(hash): | |
return f'{hash[0:6]}…{hash[-6:]}' | |
def output_to_file(hash, palette, out): | |
if len(palette) < 2: | |
return | |
if len(palette) <= 16: | |
out.write(f'<div class="pal-box"><div class="pal-container"><div>') | |
for e in palette: | |
out.write(f'\t<div title="{tohex(e)}" style="background-color: {tohex(e)}" class="special-{len(palette)}"></div>\n') | |
else: | |
palette_grid_name = 'palette-multi-l' if len(palette) > 32 else 'palette-multi' | |
out.write(f'<div class="pal-box"><div class="pal-container"><div class="{palette_grid_name}">\n') | |
num_rows = (len(palette) + 15) // 16 | |
for i in range(num_rows): | |
for j in range(16): | |
index = i * 16 + j | |
if index < len(palette): | |
e = palette[index] | |
out.write(f'\t<div title="{tohex(e)}" style="background-color: {tohex(e)}"></div>\n') | |
else: | |
out.write(f'\t<div></div>\n') | |
out.write(f'</div></div>\n<p>tx: <a href="https://symbol.fyi/transactions/{hash}">{short(hash)}</a></p></div>\n\n') | |
def generate_output(hash, palette): | |
with open('palettes.html', 'a', encoding='utf8') as output_file: | |
output_to_file(hash, palette, output_file) | |
def process_plte(hash, height, id): | |
file_path = Path(f'plte_{height}_{id}.dat') | |
with file_path.open('rb', buffering=0) as input_file: | |
plte = input_file.read() | |
view = memoryview(plte) | |
length = int.from_bytes(view[0:4], byteorder='big', signed=False) | |
if length != len(view) - 8 - 4: | |
raise RuntimeError('invalid chunk size') | |
if 0 != length % 3: | |
raise RuntimeError('chunk size is not multiple of 3') | |
num_entries = length // 3 | |
chunk_data = view[8:8+length] | |
palette = [] | |
#print(num_entries) | |
for i in range(num_entries): | |
r, g, b = chunk_data[:3] | |
palette.append((r,g,b)) | |
chunk_data = chunk_data[3:] | |
generate_output(hash, palette) | |
def main(): | |
with open('header.html', 'r', encoding='utf8') as input_file: | |
with open('palettes.html', 'w', encoding='utf8') as output_file: | |
output_file.write(input_file.read()) | |
output_file.write(f'<h2>Last generated {datetime.datetime.utcnow()} UTC</h2>\n') | |
with open('metadata.csv', 'r', encoding='utf8') as input_file: | |
r = csv.reader(input_file, delimiter=';') | |
for entry in r: | |
process_plte(*entry[1:]) | |
print(f'palettes.html generated ✅') | |
if '__main__' == __name__: | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment