diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2024-02-11 11:34:58 +0100 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2024-02-11 11:36:03 +0100 |
commit | e03dfd3b813929067e5b5c30752e336ea2be1f70 (patch) | |
tree | c0e0051a087bf00359fbbfe2f26f982d0307a035 /forms/colour_chart.py | |
parent | 507f0c2e2d5119d0c133b338ee186c2b7c806d93 (diff) |
Start writing a colour chart
Diffstat (limited to 'forms/colour_chart.py')
-rwxr-xr-x | forms/colour_chart.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/forms/colour_chart.py b/forms/colour_chart.py new file mode 100755 index 0000000..30d97d7 --- /dev/null +++ b/forms/colour_chart.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 + +import argparse +import math + +import fpdf +import strictyaml + + +class Chart: + COLUMNS=4 + ROWS=16 + + def __init__(self): + self.parser = self.get_parser() + self.args = self.parser.parse_args() + self.data = strictyaml.load(self.args.chart_data.read()).data + + def get_parser(self): + parser = argparse.ArgumentParser() + parser.add_argument( + "chart_data", + type=argparse.FileType("r") + ) + return parser + + def get_arranged_cells(self): + per_page = self.COLUMNS * self.ROWS + padded = self.data["colours"] + [{}] * ( + per_page - len(self.data["colours"]) % per_page + ) + pages = [ + padded[i * per_page:i * per_page + per_page] + for i in range(len(padded) // per_page) + ] + cells = [ + [ + [p[i + self.ROWS * j] for j in range(self.COLUMNS)] + for i in range(len(p) // self.COLUMNS) + ] + for p in pages + ] + return(cells) + + def write_pdf(self, fname): + pdf = fpdf.FPDF(format="A5") + + for p_num, page in enumerate(self.get_arranged_cells()): + pdf.add_page() + pdf.set_font("helvetica", size=12) + pdf.cell(64, 8, self.data["title"], align="C") + pdf.ln() + pdf.set_font("helvetica", "B", 12) + for j in range(self.COLUMNS): + pdf.cell(16, 8, str(j + p_num * self.COLUMNS),) + pdf.cell(16, 8, "") + pdf.ln() + for row in page: + for cell in row: + if cell: + pdf.set_fill_color(255,255,255) + if cell.get("bg"): + pdf.set_fill_color(*map(int, cell["bg"].split())) + pdf.cell(16, 8, "", border=1, fill=True) + pdf.cell(16, 8, cell["label"], ) + pdf.ln(10) + + pdf.output(fname) + + +if __name__ == "__main__": + Chart().write_pdf("asd.pdf") |