This repository was archived by the owner on Jul 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathto_hcl.py
41 lines (38 loc) · 1.5 KB
/
to_hcl.py
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
#!/usr/bin/env python3
import argparse
import csv
import platform
import numpy as np
import colorspacious
parser = argparse.ArgumentParser(
description="Sort color sets by HCL (hue, chroma, luminance) [CAM02-UCS based].",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"input", metavar="INPUT", help="Color sets to be sorted (space separated)"
)
args = parser.parse_args()
with open(args.input) as csv_file:
with open(args.input.split(".")[0] + "_hcl_sorted.txt", "w") as outfile:
# Copy header rows
outfile.write(csv_file.readline())
outfile.write(csv_file.readline())
outfile.write(csv_file.readline())
# Record environment
outfile.write("# Python " + platform.sys.version.replace("\n", "") + "\n")
outfile.write(
f"# NumPy {np.__version__}, Colorspacious {colorspacious.__version__}\n"
)
csv_reader = csv.reader(csv_file, delimiter=" ")
for row in csv_reader:
row = [i.strip() for i in row]
rgb = [(int(i[:2], 16), int(i[2:4], 16), int(i[4:], 16)) for i in row]
jab = [colorspacious.cspace_convert(i, "sRGB255", "CAM02-UCS") for i in rgb]
hcl = np.array(
[
[np.arctan2(i[2], i[1]), np.sqrt(i[1] ** 2 + i[2] ** 2), i[0]]
for i in jab
]
)
new_row = " ".join(np.array(row)[np.lexsort(hcl[:, ::-1].T)])
outfile.write(new_row + "\n")