-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.py
85 lines (64 loc) · 2.06 KB
/
script.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import colorsys
import csv
from pathlib import Path
import sys
from typing import NamedTuple
from rich.console import Console
from rich.table import Table
from data import download_data, colors_csv_file
console = Console()
error_console = Console(stderr=True, style="bold red")
FULL_COLOR_HEX_LEN = 7
class Hls(NamedTuple):
H: float
L: float
S: float
class Color(NamedTuple):
hex_: str
name: str
hls: Hls
def get_hex_colors(search_term):
if not Path(colors_csv_file).exists():
download_data()
with open(colors_csv_file) as f:
rows = csv.DictReader(f)
for row in rows:
hex_, name = row["hex"], row["name"]
if len(hex_) != FULL_COLOR_HEX_LEN:
continue
if search_term.lower() not in name.lower():
continue
hls = Hls(*colorsys.rgb_to_hls(
int(row["r"]), int(row["g"]), int(row["b"])
))
yield Color(hex_, name, hls)
def show_colors(seach_term, colors, num_column_pairs=3, order_by_hls=True):
if order_by_hls:
colors.sort(key=lambda x: x.hls.L, reverse=True)
table = Table(title=f"Matching colors for {search_term}")
for _ in range(num_column_pairs):
table.add_column("Hex")
table.add_column("Name")
def _color(hex_, string):
return f"[{hex_}]{string}"
row = []
for i, color in enumerate(colors, start=1):
row.extend([
_color(color.hex_, color.hex_),
_color(color.hex_, color.name)
])
is_last_row = i == len(colors) # in case < num_column_pairs results
if i % num_column_pairs == 0 or is_last_row:
table.add_row(*row)
row = []
console.print(table)
if __name__ == "__main__":
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} search_term")
sys.exit(1)
search_term = sys.argv[1]
colors = list(get_hex_colors(search_term))
if colors:
show_colors(search_term, colors)
else:
error_console.print(f"No matches for {search_term}")