diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-06-14 22:50:55 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-14 22:50:55 +1000 |
| commit | ee7291b7f64a359d17eb1d050086ef5357d79055 (patch) | |
| tree | f1c200d8c8ba81030cbb2113a2be122db6508c8f /src/caelestia/utils/material/__init__.py | |
| parent | Merge pull request #5 from dalpax/patch-1 (diff) | |
| parent | Merge branch 'main' into python-rework (diff) | |
| download | caelestia-cli-ee7291b7f64a359d17eb1d050086ef5357d79055.tar.gz caelestia-cli-ee7291b7f64a359d17eb1d050086ef5357d79055.tar.bz2 caelestia-cli-ee7291b7f64a359d17eb1d050086ef5357d79055.zip | |
Merge pull request #6 from caelestia-dots/python-rework
feat: rewrite in python
Diffstat (limited to 'src/caelestia/utils/material/__init__.py')
| -rw-r--r-- | src/caelestia/utils/material/__init__.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/caelestia/utils/material/__init__.py b/src/caelestia/utils/material/__init__.py new file mode 100644 index 0000000..8adab1f --- /dev/null +++ b/src/caelestia/utils/material/__init__.py @@ -0,0 +1,52 @@ +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: 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(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: str = str(wallpaper_thumbnail_path), scheme=None) -> dict[str, str]: + if scheme is None: + from caelestia.utils.scheme import get_scheme + + scheme = get_scheme() + + cache_base = scheme_cache_dir / compute_hash(image) + cache = (cache_base / scheme.variant / scheme.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(scheme.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 |