[!B] wallpaper: smart variant

This commit is contained in:
2 * r + 2 * t
2025-06-24 00:30:52 +10:00
parent 2d05515b56
commit affa3bfd21
+24 -12
View File
@@ -1,3 +1,4 @@
import json
import random import random
from argparse import Namespace from argparse import Namespace
from pathlib import Path from pathlib import Path
@@ -71,20 +72,28 @@ def get_thumb(wall: Path, cache: Path) -> Path:
return thumb return thumb
def get_smart_mode(wall: Path, cache: Path) -> str: def get_smart_opts(wall: Path, cache: Path) -> str:
mode_cache = cache / "mode.txt" opts_cache = cache / "smart.json"
try: try:
return mode_cache.read_text() return json.loads(opts_cache.read_text())
except IOError: except (IOError, json.JSONDecodeError):
pass
with Image.open(get_thumb(wall, cache)) as img: with Image.open(get_thumb(wall, cache)) as img:
img.thumbnail((1, 1), Image.LANCZOS) img.thumbnail((1, 1), Image.LANCZOS)
mode = "light" if Hct.from_int(argb_from_rgb(*img.getpixel((0, 0)))).tone > 60 else "dark" hct = Hct.from_int(argb_from_rgb(*img.getpixel((0, 0))))
mode_cache.parent.mkdir(parents=True, exist_ok=True) opts = {
mode_cache.write_text(mode) "mode": "light" if hct.tone > 60 else "dark",
"variant": "neutral" if hct.chroma < 20 else "tonalspot",
}
return mode opts_cache.parent.mkdir(parents=True, exist_ok=True)
with opts_cache.open("w") as f:
json.dump(opts, f)
return opts
def get_colours_for_wall(wall: Path | str, no_smart: bool) -> None: 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" name = "dynamic"
if not no_smart: if not no_smart:
smart_opts = get_smart_opts(wall, cache)
scheme = Scheme( scheme = Scheme(
{ {
"name": name, "name": name,
"flavour": "default", "flavour": "default",
"mode": get_smart_mode(wall, cache), "mode": smart_opts["mode"],
"variant": scheme.variant, "variant": smart_opts["variant"],
"colours": scheme.colours, "colours": scheme.colours,
} }
) )
@@ -134,9 +144,11 @@ def set_wallpaper(wall: Path | str, no_smart: bool) -> None:
scheme = get_scheme() 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: 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 # Update colours
scheme.update_colours() scheme.update_colours()