| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
""" |
| 4 |
(brewer_colors, svgcolor_names, color_names) → colortbl.h generator |
| 5 |
""" |
| 6 |
|
| 7 |
import argparse |
| 8 |
import locale |
| 9 |
import re |
| 10 |
import sys |
| 11 |
|
| 12 |
|
| 13 |
def rgb_to_hsv(r: int, g: int, b: int) -> tuple[int, int, int]: |
| 14 |
"""transform an RGB color into HSV format""" |
| 15 |
|
| 16 |
r /= 255.0 |
| 17 |
g /= 255.0 |
| 18 |
b /= 255.0 |
| 19 |
|
| 20 |
max_ = max((r, g, b)) |
| 21 |
min_ = min((r, g, b)) |
| 22 |
|
| 23 |
v = max_ |
| 24 |
|
| 25 |
if max_ != 0: |
| 26 |
s = (max_ - min_) / max_ |
| 27 |
else: |
| 28 |
s = 0 |
| 29 |
|
| 30 |
if s == 0: |
| 31 |
h = 0 |
| 32 |
else: |
| 33 |
delta = max_ - min_ |
| 34 |
rc = (max_ - r) / delta |
| 35 |
gc = (max_ - g) / delta |
| 36 |
bc = (max_ - b) / delta |
| 37 |
if r == max_: |
| 38 |
h = bc - gc |
| 39 |
elif g == max_: |
| 40 |
h = 2.0 + (rc - bc) |
| 41 |
else: |
| 42 |
h = 4.0 + (gc - rc) |
| 43 |
h *= 60.0 |
| 44 |
if h < 0.0: |
| 45 |
h += 360.0 |
| 46 |
|
| 47 |
return int(h / 360.0 * 255.0), int(s * 255.0), int(v * 255.0) |
| 48 |
|
| 49 |
|
| 50 |
def main(args: list[str]) -> int: |
| 51 |
"""entry point""" |
| 52 |
|
| 53 |
# avoid sorting issues due to locale differences |
| 54 |
locale.setlocale(locale.LC_ALL, "C") |
| 55 |
|
| 56 |
# parse command line arguments |
| 57 |
parser = argparse.ArgumentParser(description=__doc__) |
| 58 |
parser.add_argument( |
| 59 |
"brewer_colors", |
| 60 |
type=argparse.FileType("rt"), |
| 61 |
help="input Brewer color table entries", |
| 62 |
) |
| 63 |
parser.add_argument( |
| 64 |
"svgcolor_names", |
| 65 |
type=argparse.FileType("rt"), |
| 66 |
help="input SVG color table entries", |
| 67 |
) |
| 68 |
parser.add_argument( |
| 69 |
"color_names", |
| 70 |
type=argparse.FileType("rt"), |
| 71 |
help="input other color table entries", |
| 72 |
) |
| 73 |
parser.add_argument( |
| 74 |
"output", type=argparse.FileType("wt"), help="output color table C header" |
| 75 |
) |
| 76 |
options = parser.parse_args(args[1:]) |
| 77 |
|
| 78 |
options.output.write("static const hsvrgbacolor_t color_lib[] = {\n") |
| 79 |
|
| 80 |
# collect all color entries |
| 81 |
entries = ( |
| 82 |
options.brewer_colors.readlines() |
| 83 |
+ options.svgcolor_names.readlines() |
| 84 |
+ options.color_names.readlines() |
| 85 |
) |
| 86 |
|
| 87 |
# sort these so `bsearch` for a color will work correctly |
| 88 |
entries = sorted(entries, key=locale.strxfrm) |
| 89 |
|
| 90 |
for entry in entries: |
| 91 |
# ignore blank lines |
| 92 |
if entry.strip() == "": |
| 93 |
continue |
| 94 |
# ignore commented lines |
| 95 |
if re.match(r"\s*#", entry) is not None: |
| 96 |
continue |
| 97 |
|
| 98 |
# extract fields of the entry |
| 99 |
m = re.match( |
| 100 |
r"(?P<name>\S+) (?P<r>\d+) (?P<g>\d+) (?P<b>\d+) (?P<a>\d+)", entry |
| 101 |
) |
| 102 |
assert m is not None, f'unexpected entry format "{entry}"' |
| 103 |
|
| 104 |
# transform an RGB color into HSV |
| 105 |
r = int(m.group("r")) |
| 106 |
g = int(m.group("g")) |
| 107 |
b = int(m.group("b")) |
| 108 |
h, s, v = rgb_to_hsv(r, g, b) |
| 109 |
|
| 110 |
# write this out as a C array entry |
| 111 |
name = m.group("name") |
| 112 |
a = int(m.group("a")) |
| 113 |
options.output.write(f'{{"{name}",{h},{s},{v},{r},{g},{b},{a}}},\n') |
| 114 |
|
| 115 |
options.output.write("};\n") |
| 116 |
|
| 117 |
return 0 |
| 118 |
|
| 119 |
|
| 120 |
if __name__ == "__main__": |
| 121 |
sys.exit(main(sys.argv)) |