forked from cosmos/chain-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primary_colors.py
72 lines (50 loc) · 1.84 KB
/
primary_colors.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
from colorthief import ColorThief
import pathlib
import json
chain_registry = pathlib.Path(".")
def get_primary_color(png):
color_thief = ColorThief(png)
dominant_color = color_thief.get_color(quality=1)
return "#%02x%02x%02x" % dominant_color
def add_primary_color_to_image(image):
if "png" not in image.keys():
return
if "theme" in image.keys() and "primary_color_hex" in image["theme"].keys():
return
png = image["png"]
png = png.replace(
"https://raw.githubusercontent.com/cosmos/chain-registry/master",
".",
)
try:
hex = get_primary_color(png)
except:
return
image.setdefault("theme", {})["primary_color_hex"] = hex
print(image["theme"]["primary_color_hex"])
return image
for item in chain_registry.rglob("*"):
if (
item.is_file()
and ("assetlist.json" in item.name or "chain.json" in item.name)
and "_template" not in item.parts
and "_IBC" not in item.parts
and "testnets" not in item.parts
):
with open(item, "r+", encoding="utf-8") as f:
data = json.load(f)
if "images" in data.keys():
for image in data["images"]:
new_image = add_primary_color_to_image(image)
if new_image:
image = new_image
if "assets" in data.keys():
for asset in data["assets"]:
print(asset["symbol"])
if "images" in asset.keys():
for image in asset["images"]:
new_image = add_primary_color_to_image(image)
if new_image:
image = new_image
print(data)
item.write_text(json.dumps(data, indent=2, ensure_ascii=False))