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
|
import json
from pathlib import Path
from materialyoucolor.hct import Hct
from caelestia.utils.material.generator import gen_scheme
from caelestia.utils.material.score import score
from caelestia.utils.paths import compute_hash, scheme_cache_dir, wallpaper_thumbnail_path
def get_score_for_image(image: Path | str, cache_base: Path) -> tuple[list[Hct], list[Hct]]:
cache = cache_base / "score.json"
try:
with cache.open("r") as f:
return [[Hct.from_int(c) for c in li] for li in json.load(f)]
except (IOError, json.JSONDecodeError):
pass
s = score(str(image))
cache.parent.mkdir(parents=True, exist_ok=True)
with cache.open("w") as f:
json.dump([[c.to_int() for c in li] for li in s], f)
return s
def get_colours_for_image(image: Path | str = wallpaper_thumbnail_path, scheme=None) -> dict[str, str]:
if scheme is None:
from caelestia.utils.scheme import get_scheme
scheme = get_scheme()
flavour = scheme.flavour if scheme.flavour in ("default", "alt1", "alt2") else "default"
cache_base = scheme_cache_dir / compute_hash(image)
cache = (cache_base / scheme.variant / flavour / scheme.mode).with_suffix(".json")
try:
with cache.open("r") as f:
return json.load(f)
except (IOError, json.JSONDecodeError):
pass
primaries, colours = get_score_for_image(image, cache_base)
i = ["default", "alt1", "alt2"].index(flavour)
scheme = gen_scheme(scheme, primaries[i], colours)
cache.parent.mkdir(parents=True, exist_ok=True)
with cache.open("w") as f:
json.dump(scheme, f)
return scheme
|