diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-06-24 00:30:52 +1000 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-06-24 00:30:52 +1000 |
| commit | affa3bfd21d844bafe7b4f90e2cdda4ef0932cc7 (patch) | |
| tree | 2e7830b6119c0fff43704992700d4b7f3c1d42ea /src/caelestia/utils/wallpaper.py | |
| parent | scheme: reduce chroma for neutral variant (diff) | |
| download | caelestia-cli-affa3bfd21d844bafe7b4f90e2cdda4ef0932cc7.tar.gz caelestia-cli-affa3bfd21d844bafe7b4f90e2cdda4ef0932cc7.tar.bz2 caelestia-cli-affa3bfd21d844bafe7b4f90e2cdda4ef0932cc7.zip | |
[!B] wallpaper: smart variant
Diffstat (limited to 'src/caelestia/utils/wallpaper.py')
| -rw-r--r-- | src/caelestia/utils/wallpaper.py | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/src/caelestia/utils/wallpaper.py b/src/caelestia/utils/wallpaper.py index 300d746..d3550ab 100644 --- a/src/caelestia/utils/wallpaper.py +++ b/src/caelestia/utils/wallpaper.py @@ -1,3 +1,4 @@ +import json import random from argparse import Namespace from pathlib import Path @@ -71,20 +72,28 @@ def get_thumb(wall: Path, cache: Path) -> Path: return thumb -def get_smart_mode(wall: Path, cache: Path) -> str: - mode_cache = cache / "mode.txt" +def get_smart_opts(wall: Path, cache: Path) -> str: + opts_cache = cache / "smart.json" try: - return mode_cache.read_text() - except IOError: - with Image.open(get_thumb(wall, cache)) as img: - img.thumbnail((1, 1), Image.LANCZOS) - mode = "light" if Hct.from_int(argb_from_rgb(*img.getpixel((0, 0)))).tone > 60 else "dark" + return json.loads(opts_cache.read_text()) + except (IOError, json.JSONDecodeError): + pass + + with Image.open(get_thumb(wall, cache)) as img: + img.thumbnail((1, 1), Image.LANCZOS) + hct = Hct.from_int(argb_from_rgb(*img.getpixel((0, 0)))) + + opts = { + "mode": "light" if hct.tone > 60 else "dark", + "variant": "neutral" if hct.chroma < 20 else "tonalspot", + } - mode_cache.parent.mkdir(parents=True, exist_ok=True) - mode_cache.write_text(mode) + opts_cache.parent.mkdir(parents=True, exist_ok=True) + with opts_cache.open("w") as f: + json.dump(opts, f) - return mode + return opts def get_colours_for_wall(wall: Path | str, no_smart: bool) -> None: @@ -94,12 +103,13 @@ def get_colours_for_wall(wall: Path | str, no_smart: bool) -> None: name = "dynamic" if not no_smart: + smart_opts = get_smart_opts(wall, cache) scheme = Scheme( { "name": name, "flavour": "default", - "mode": get_smart_mode(wall, cache), - "variant": scheme.variant, + "mode": smart_opts["mode"], + "variant": smart_opts["variant"], "colours": scheme.colours, } ) @@ -134,9 +144,11 @@ def set_wallpaper(wall: Path | str, no_smart: bool) -> None: scheme = get_scheme() - # Change mode based on wallpaper colour + # Change mode and variant based on wallpaper colour if scheme.name == "dynamic" and not no_smart: - scheme.mode = get_smart_mode(wall, cache) + smart_opts = get_smart_opts(wall, cache) + scheme.mode = smart_opts["mode"] + scheme.variant = smart_opts["variant"] # Update colours scheme.update_colours() |