This repository has been archived on 2026-11-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
dotfiles/config/nushell/scripts/wallpaper-theme.nu
T

187 lines
6.3 KiB
Nu

#!/usr/bin/env nu
# Derives a Catppuccin flavor + accent from a wallpaper's average color and
# applies it system-wide. No manual override - the wallpaper is the source of truth.
def palette [] {
open ($"($env.HOME)/.dotfiles/config/themes/catppuccin/palette.json")
}
def average-color [path: string] {
let out = (magick $path -resize 1x1 txt:- | lines | skip 1 | first)
let matches = ($out | parse --regex '\((?P<r>\d+),(?P<g>\d+),(?P<b>\d+)\)')
if ($matches | is-empty) {
error make { msg: $"could not read average color from ($path)" }
}
let m = ($matches | first)
{ r: ($m.r | into int), g: ($m.g | into int), b: ($m.b | into int) }
}
def rgb-to-hsl [rgb: record<r: int, g: int, b: int>] {
let r = $rgb.r / 255
let g = $rgb.g / 255
let b = $rgb.b / 255
let max = ([$r $g $b] | math max)
let min = ([$r $g $b] | math min)
let l = ($max + $min) / 2
let delta = $max - $min
let s = if $delta == 0 {
0
} else {
$delta / (1 - (($l * 2 - 1) | math abs))
}
let h = if $delta == 0 {
0
} else if $max == $r {
(60 * ((($g - $b) / $delta) mod 6))
} else if $max == $g {
(60 * ((($b - $r) / $delta) + 2))
} else {
(60 * ((($r - $g) / $delta) + 4))
}
let h = if $h < 0 { $h + 360 } else { $h }
{ h: $h, s: $s, l: $l }
}
def hex-byte [] {
# "into int -r 16" misreads byte pairs like "0b" as a binary literal prefix.
$"0x($in)" | into int
}
def hue-dist [a: float, b: float] {
let diff = (($a - $b) | math abs)
[$diff, 360 - $diff] | math min
}
def pick-flavor [rgb: record<r: int, g: int, b: int>] {
let luma = (0.299 * $rgb.r + 0.587 * $rgb.g + 0.114 * $rgb.b) / 255
if $luma < 0.5 { "mocha" } else { "latte" }
}
def pick-accent [flavor: string, rgb: record<r: int, g: int, b: int>] {
let wallpaper_hsl = (rgb-to-hsl $rgb)
# Near-gray wallpapers don't carry a meaningful hue - fall back to mauve.
if $wallpaper_hsl.s < 0.12 {
return "mauve"
}
let accents = (palette | get $flavor | get accents)
$accents
| columns
| each {|name|
let hex = ($accents | get $name)
let r = ($hex | str substring 1..2 | hex-byte)
let g = ($hex | str substring 3..4 | hex-byte)
let b = ($hex | str substring 5..6 | hex-byte)
let accent_hsl = (rgb-to-hsl { r: $r, g: $g, b: $b })
{ name: $name, dist: (hue-dist $wallpaper_hsl.h $accent_hsl.h) }
}
| sort-by dist
| first
| get name
}
def write-theme-colors [flavor: string, accent_name: string] {
let p = (palette | get $flavor)
let accent = ($p.accents | get $accent_name)
let border = $"#1a($accent | str substring 1..)" # accent-tinted, ~10% opacity
let colors = [
$" property color background: \"#e6($p.crust | str substring 1..)\""
$" property color surface: \"#cc($p.mantle | str substring 1..)\""
$" property color surfaceHover: \"#f2($p.surface0 | str substring 1..)\""
$" property color overlay: \"#60($p.crust | str substring 1..)\""
""
$" property color primary: \"($accent)\""
$" property color secondary: \"($p.accents.lavender)\""
$" property color accent: \"($accent)\""
$" property color alert: \"($p.red)\""
$" property color success: \"($p.green)\""
$" property color warning: \"($p.yellow)\""
""
$" property color text: \"($p.text)\""
$" property color textMuted: \"($p.subtext0)\""
""
$" property color border: \"($border)\""
]
let theme_path = $"($env.HOME)/.config/quickshell/components/Shared/Theme.qml"
let lines = (open --raw $theme_path | lines)
let begin = ($lines | enumerate | where item == " // GENERATED:COLORS:BEGIN" | get index.0)
let end = ($lines | enumerate | where item == " // GENERATED:COLORS:END" | get index.0)
let new_lines = (
($lines | take ($begin + 1))
++ [" property QtObject colors: QtObject {"]
++ $colors
++ [" }"]
++ ($lines | skip $end)
)
($new_lines | str join "\n") + "\n" | save -f $theme_path
}
def propagate [flavor: string] {
let home = $env.HOME
let base = $"($home)/.config/themes/catppuccin/($flavor)"
let gtk_theme = (open $"($base)/gtk-theme-name" | str trim)
$flavor | save -f $"($home)/.config/current-theme"
let wanted_darkman = if $flavor == "mocha" { "dark" } else { "light" }
let current_darkman = (try { darkman get | str trim } catch { "" })
if $current_darkman != "" and $current_darkman != $wanted_darkman {
try { bash -c $"darkman set ($wanted_darkman) &" } catch {}
}
# Hypr palette. Configs must source ~/.config/hypr/theme.conf to use this.
ln -sfn $"($base)/hypr.conf" $"($home)/.config/hypr/theme.conf"
# Mako colors, only if mako config dir exists.
if ($"($home)/.config/mako" | path exists) {
ln -sfn $"($base)/mako.conf" $"($home)/.config/mako/config"
if (which makoctl | is-not-empty) { makoctl reload }
}
# Btop theme
if ($"($home)/.config/btop/btop.conf" | path exists) {
bash -lc $"sed -i 's|color_theme = \".*\"|color_theme = \"($home)/.config/btop/themes/catppuccin_($flavor).theme\"|' \"($home)/.config/btop/btop.conf\""
}
# Helix theme
if ($"($home)/.config/helix/config.toml" | path exists) {
bash -lc $"sed -i 's/theme = \"catppuccin_.*_transparent\"/theme = \"catppuccin_($flavor)_transparent\"/' \"($home)/.config/helix/config.toml\""
}
# Yazi theme
if ($"($home)/.config/yazi" | path exists) {
$"[flavor]\nuse = \"catppuccin-($flavor)\"\n" | save -f $"($home)/.config/yazi/theme.toml"
}
# GTK theme, only if installed.
try {
bash -lc $"gsettings set org.gnome.desktop.interface gtk-theme ($gtk_theme)"
let color_scheme = if $flavor == "mocha" { "prefer-dark" } else { "prefer-light" }
bash -lc $"gsettings set org.gnome.desktop.interface color-scheme ($color_scheme)"
try { mkdir $"($home)/.config/gtk-3.0" } catch {}
let prefer_dark = if $flavor == "mocha" { "1" } else { "0" }
$"[Settings]\ngtk-theme-name=($gtk_theme)\ngtk-application-prefer-dark-theme=($prefer_dark)\n" | save -f $"($home)/.config/gtk-3.0/settings.ini"
} catch {}
hyprctl reload
bash -lc "pkill quickshell 2>/dev/null; setsid quickshell >/tmp/quickshell-theme.log 2>&1 < /dev/null &"
}
def main [wallpaper_path: string] {
let rgb = (average-color $wallpaper_path)
let flavor = (pick-flavor $rgb)
let accent = (pick-accent $flavor $rgb)
write-theme-colors $flavor $accent
propagate $flavor
}