-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplater.py
executable file
·103 lines (82 loc) · 2.91 KB
/
templater.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
"""Convert hex color code to catppuccin (for now) template."""
import argparse
import re
def hex_to_rgb(hex_color: str) -> tuple[int, ...]:
"""Convert hex color to RGB tuple."""
hex_color = hex_color.lstrip("#")
return tuple(int(hex_color[i : i + 2], 16) for i in (0, 2, 4))
def rgb_to_hex(rgb: tuple[int, ...]) -> str:
"""Convert RGB tuple to hex color."""
return "#{:02x}{:02x}{:02x}".format(*rgb)
def mix_colors(color1: str, color2: str, ratio: float) -> str:
"""Mix two hex colors based on the given ratio."""
rgb1 = hex_to_rgb(color1)
rgb2 = hex_to_rgb(color2)
mixed_rgb = tuple(
round((1 - ratio) * c1 + ratio * c2) for c1, c2 in zip(rgb1, rgb2)
)
return rgb_to_hex(mixed_rgb)
# Define the color scheme
color_scheme = {
"#f5e0dc": "rosewater",
"#f2cdcd": "flamingo",
"#f5c2e7": "pink",
"#cba6f7": "mauve",
"#f38ba8": "red",
"#eba0ac": "maroon",
"#fab387": "peach",
"#f9e2af": "yellow",
"#a6e3a1": "green",
"#94e2d5": "teal",
"#89dceb": "sky",
"#74c7ec": "sapphire",
"#89b4fa": "blue",
"#b4befe": "lavender",
"#cdd6f4": "text",
"#bac2de": "subtext1",
"#a6adc8": "subtext0",
"#9399b2": "overlay2",
"#7f849c": "overlay1",
"#6c7086": "overlay0",
"#585b70": "surface2",
"#45475a": "surface1",
"#313244": "surface0",
"#1e1e2e": "base",
"#181825": "mantle",
"#11111b": "crust",
}
def convert_hex_to_template(input_file: str, output_file: str, quote: bool) -> None:
"""Transform hex to templates."""
with open(input_file, "r") as file:
data = file.read()
# Find all hex color codes in the data
hex_codes: list[str] = re.findall(r"#[0-9a-fA-F]{6}", data)
for hex_code in hex_codes:
matcher = hex_code.lower()
if matcher in color_scheme:
if quote:
template = f"{{{{ .colorScheme.{color_scheme[matcher]} | quote }}}}"
data = re.sub(rf'["\']?{hex_code}["\']?', template, data)
else:
template = f"{{{{ .colorScheme.{color_scheme[matcher]} }}}}"
data = data.replace(hex_code, template)
else:
print(f"Hex code {hex_code} not found in color scheme")
with open(output_file, "w") as file:
_ = file.write(data)
print("Conversion successful")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Convert hex color codes to template format."
)
parser.add_argument("--input", help="The input file containing hex color codes.")
parser.add_argument("--output", help="The output file to write the converted data.")
parser.add_argument(
"--quote",
action=argparse.BooleanOptionalAction,
default=True,
help="Do not use | quote in the template.",
)
args = parser.parse_args()
convert_hex_to_template(args.input, args.output, args.quote)