1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
"""
Script for translating the .txt RGB files in
http://www.fabiocrameri.ch/colourmaps.php to a colordata.py file.
This means converting 0-1 floats to 0-255 integers and formatting the
data as lists of lists.
"""
import sys
from pathlib import Path
from typing import Iterable, TextIO, Tuple
import click
def float_rgb_to_int(value: float) -> int:
return round(value * 255)
def read_rgb_txt(filename: Path) -> Iterable[Tuple[float, float, float]]:
with filename.open('r') as fp:
yield from (
tuple(float_rgb_to_int(float(x)) for x in row.split()) for row in fp
)
def process_file(filename: str, dest: TextIO):
filename: Path = Path(filename)
dest.write(f'{filename.stem.upper()} = (\n')
for rgb in read_rgb_txt(filename):
dest.write(f' {rgb},\n')
dest.write(')\n\n')
@click.command()
@click.argument("dest", type=click.File("w"))
@click.argument("sources", nargs=-1)
def main(dest: TextIO, sources: Tuple):
dest.write('# Via http://www.fabiocrameri.ch/colourmaps.php\n\n')
for filename in sources:
process_file(filename, dest)
if __name__ == "__main__":
sys.exit(main())
|