summaryrefslogtreecommitdiff
path: root/.config/niri/apply-colors.sh
diff options
context:
space:
mode:
Diffstat (limited to '.config/niri/apply-colors.sh')
-rwxr-xr-x.config/niri/apply-colors.sh84
1 files changed, 84 insertions, 0 deletions
diff --git a/.config/niri/apply-colors.sh b/.config/niri/apply-colors.sh
new file mode 100755
index 0000000..505578c
--- /dev/null
+++ b/.config/niri/apply-colors.sh
@@ -0,0 +1,84 @@
+#!/bin/bash
+# Apply a wallpaper and generate colors with wallust (16-color palette) + matugen (Material You / M3).
+# Usage: apply-colors.sh <image> [light|dark]
+# Called by theme-switcher.sh and the waybar random-wallpaper click.
+
+set -euo pipefail
+
+SCRIPT_DIR="$(cd "$(dirname "$(realpath "$0")")" && pwd)"
+IMG="${1:-}"
+MODE="${2:-auto}"
+LOG="${THEME_LOG:-/tmp/theme-switcher.log}"
+
+log() {
+ printf '%s %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" >>"$LOG"
+}
+
+# Record the exact failing command + line on any error.
+trap 'log "apply-colors FAILED (exit $?) in ${BASH_SOURCE[0]}:$LINENO: $BASH_COMMAND"' ERR
+
+if [ -z "$IMG" ] || [ ! -f "$IMG" ]; then
+ echo "usage: $0 <image> [light|dark]" >&2
+ exit 1
+fi
+
+# wait for the awww daemon to be ready (it is spawned next to this script at login)
+log "awww img $IMG"
+for _ in $(seq 1 10); do
+ if awww query >/dev/null 2>&1; then
+ break
+ fi
+ sleep 1
+done
+
+# set the wallpaper FIRST and unconditionally — this is the critical, must-work
+# step. Color generation below is best-effort: a failure there must never block
+# the wallpaper from changing (that coupling is what froze the wallpaper before).
+awww img "$IMG" || { log "awww img FAILED ($IMG)"; exit 1; }
+
+# blurred copy for the Overview backdrop (sibling or on-the-fly generation)
+"$SCRIPT_DIR/set-backdrop.sh" "$IMG" || log "set-backdrop FAILED ($IMG)"
+
+# wallust: classic 16-color palette (foot, btop, ...)
+# fastresize (the config default) is flaky on some images — always use resized.
+# Best-effort: a wallust failure must not prevent the wallpaper from sticking.
+log "wallust palette=$MODE $IMG"
+case "$MODE" in
+light|dark) WALLUST_PALETTE=("--palette" "$MODE") ;;
+*) WALLUST_PALETTE=() ;;
+esac
+# wallust's `resized` backend is intermittently flaky on some images; retry a
+# few times, but ultimately tolerate failure.
+ok=0
+for attempt in 1 2 3; do
+ if wallust run -b resized "${WALLUST_PALETTE[@]}" "$IMG"; then
+ ok=1
+ break
+ fi
+ log "wallust attempt $attempt FAILED ($IMG, mode=$MODE)"
+ [ "$attempt" -lt 3 ] && sleep 2
+done
+if [ "$ok" -ne 1 ]; then
+ log "wallust FAILED after 3 attempts ($IMG, mode=$MODE) — continuing without wallust colors"
+else
+ # foot needs bare RRGGBB, not #RRGGBB — strip the leading '#'
+ sed -i 's/#\([0-9a-fA-F]\)/\1/g' ~/.cache/wallust/foot-theme.ini 2>/dev/null || true
+fi
+
+# matugen: Material You / M3 colors (niri focus ring, ...)
+# --prefer is required for non-interactive runs (multiple source colors).
+# Best-effort: a matugen failure (e.g. bad config after an upgrade) must not
+# block the wallpaper.
+log "matugen mode=$MODE $IMG"
+case "$MODE" in
+light) matugen image "$IMG" -m "light" --prefer saturation ;;
+dark) matugen image "$IMG" -m "dark" --prefer saturation ;;
+*) matugen image "$IMG" --prefer saturation ;;
+esac || log "matugen FAILED ($IMG, mode=$MODE) — continuing without matugen colors"
+
+# reload consumers
+killall -SIGUSR2 waybar 2>/dev/null || true
+killall -USR1 foot 2>/dev/null || true
+niri msg action load-config-file 2>/dev/null || true
+
+log "apply-colors OK ($IMG, $MODE)"