279 lines
10 KiB
Nu
279 lines
10 KiB
Nu
#!/usr/bin/env nu
|
|
# Derives a Catppuccin flavor + a trio of accents from a wallpaper's dominant
|
|
# colors and applies it system-wide. No manual override - the wallpaper is
|
|
# the source of truth. Colors are always Catppuccin swatches - we only ever
|
|
# change *which* swatches get used, never introduce outside colors.
|
|
|
|
def palette [] {
|
|
open ($"($env.HOME)/.dotfiles/config/themes/catppuccin/palette.json")
|
|
}
|
|
|
|
def hex-byte [] {
|
|
# "into int -r 16" misreads byte pairs like "0b" as a binary literal prefix.
|
|
$"0x($in)" | into int
|
|
}
|
|
|
|
def to-hex-byte [n: int] {
|
|
let digits = "0123456789abcdef"
|
|
let hi = ($n // 16)
|
|
let lo = ($n mod 16)
|
|
$"($digits | str substring $hi..$hi)($digits | str substring $lo..$lo)"
|
|
}
|
|
|
|
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 hue-dist [a: float, b: float] {
|
|
let diff = (($a - $b) | math abs)
|
|
[$diff, 360 - $diff] | math min
|
|
}
|
|
|
|
# Wallpaper's dominant colors, ranked by pixel count (descending). Replaces
|
|
# the old single 1x1-resize average-color sample, which always produced one
|
|
# flat blend no matter how many distinct colors the wallpaper actually had.
|
|
def dominant-colors [path: string, n: int] {
|
|
let out = (magick $path -resize 100x100 -colors $n +dither -format %c histogram:info:-)
|
|
$out
|
|
| lines
|
|
| parse --regex '(?P<count>\d+):.*#(?P<hex>[0-9A-Fa-f]{6})'
|
|
| insert count_n { |row| $row.count | into int }
|
|
| sort-by count_n --reverse
|
|
| each { |row|
|
|
let r = ($row.hex | str substring 0..1 | hex-byte)
|
|
let g = ($row.hex | str substring 2..3 | hex-byte)
|
|
let b = ($row.hex | str substring 4..5 | hex-byte)
|
|
{ r: $r, g: $g, b: $b, hsl: (rgb-to-hsl { r: $r, g: $g, b: $b }) }
|
|
}
|
|
}
|
|
|
|
def accent-list [flavor: string] {
|
|
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)
|
|
{ name: $name, hsl: (rgb-to-hsl { r: $r, g: $g, b: $b }) }
|
|
}
|
|
}
|
|
|
|
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" }
|
|
}
|
|
|
|
# Maps up to `want` of the wallpaper's dominant swatches onto distinct
|
|
# Catppuccin accent swatches (at least `min_sep` degrees of hue apart), so
|
|
# the shell gets a coherent multi-accent palette instead of one flat color.
|
|
# Near-gray swatches (no meaningful hue) are skipped. If the wallpaper
|
|
# doesn't carry enough distinct hues, remaining slots are filled from a
|
|
# fixed, pleasant fallback order so the palette never collapses to
|
|
# monochrome.
|
|
def pick-accents [flavor: string, swatches: list, want: int, min_sep: float] {
|
|
let accents = (accent-list $flavor)
|
|
mut chosen_names = []
|
|
mut chosen_hues = []
|
|
|
|
for sw in $swatches {
|
|
if ($chosen_names | length) >= $want { continue }
|
|
if $sw.hsl.s < 0.12 { continue }
|
|
|
|
let names_snapshot = $chosen_names
|
|
let hues_snapshot = $chosen_hues
|
|
|
|
let candidates = (
|
|
$accents
|
|
| where {|a| not ($names_snapshot | any {|c| $c == $a.name }) }
|
|
| each {|a|
|
|
let dists_to_chosen = ($hues_snapshot | each {|ch| hue-dist $a.hsl.h $ch })
|
|
let min_dist_to_chosen = if ($dists_to_chosen | is-empty) { 999.0 } else { $dists_to_chosen | math min }
|
|
{ name: $a.name, dist_to_swatch: (hue-dist $sw.hsl.h $a.hsl.h), min_dist_to_chosen: $min_dist_to_chosen }
|
|
}
|
|
| where min_dist_to_chosen >= $min_sep
|
|
| sort-by dist_to_swatch
|
|
)
|
|
|
|
if ($candidates | is-empty) { continue }
|
|
let pick = ($candidates | first)
|
|
$chosen_names = ($chosen_names | append $pick.name)
|
|
$chosen_hues = ($chosen_hues | append (($accents | where name == $pick.name | first).hsl.h))
|
|
}
|
|
|
|
let fallback_order = ["mauve" "sapphire" "peach" "green" "pink" "yellow" "teal" "blue" "flamingo"]
|
|
for fb in $fallback_order {
|
|
if ($chosen_names | length) >= $want { continue }
|
|
if ($chosen_names | any {|c| $c == $fb }) { continue }
|
|
$chosen_names = ($chosen_names | append $fb)
|
|
}
|
|
|
|
$chosen_names
|
|
}
|
|
|
|
def blend-hex [a: string, b: string, ratio: float] {
|
|
# Opaque RGB interpolation - neo-brutalist surfaces are solid blocks, never
|
|
# alpha blends, so "muted container" colors are baked as real flat hex.
|
|
let ar = ($a | str substring 1..2 | hex-byte)
|
|
let ag = ($a | str substring 3..4 | hex-byte)
|
|
let ab = ($a | str substring 5..6 | hex-byte)
|
|
let br = ($b | str substring 1..2 | hex-byte)
|
|
let bg = ($b | str substring 3..4 | hex-byte)
|
|
let bb = ($b | str substring 5..6 | hex-byte)
|
|
let mix = {|x, y| (($x * (1 - $ratio)) + ($y * $ratio)) | math round | into int }
|
|
let r = (to-hex-byte (do $mix $ar $br))
|
|
let g = (to-hex-byte (do $mix $ag $bg))
|
|
let b2 = (to-hex-byte (do $mix $ab $bb))
|
|
$"#($r)($g)($b2)"
|
|
}
|
|
|
|
def container-tint [base: string, accent: string] {
|
|
# Opaque muted tile: ~30% accent blended into the base surface, no alpha.
|
|
blend-hex $base $accent 0.3
|
|
}
|
|
|
|
def write-theme-colors [flavor: string, accent_names: list] {
|
|
let p = (palette | get $flavor)
|
|
let primary = ($p.accents | get $accent_names.0)
|
|
let secondary = ($p.accents | get $accent_names.1)
|
|
let tertiary = ($p.accents | get $accent_names.2)
|
|
let info = $p.accents.sapphire
|
|
|
|
let colors = [
|
|
$" property color background: \"($p.base)\""
|
|
$" property color surface: \"($p.mantle)\""
|
|
$" property color surfaceHover: \"($p.surface0)\""
|
|
$" property color overlay: \"#cc($p.crust | str substring 1..)\""
|
|
""
|
|
$" property color crust: \"($p.crust)\""
|
|
$" property color outlineVariant: \"($p.surface1)\""
|
|
""
|
|
$" property color primary: \"($primary)\""
|
|
$" property color secondary: \"($secondary)\""
|
|
$" property color tertiary: \"($tertiary)\""
|
|
$" property color accent: \"($primary)\""
|
|
""
|
|
$" property color primaryContainer: \"(container-tint $p.surface0 $primary)\""
|
|
$" property color secondaryContainer: \"(container-tint $p.surface0 $secondary)\""
|
|
$" property color tertiaryContainer: \"(container-tint $p.surface0 $tertiary)\""
|
|
""
|
|
$" property color alert: \"($p.red)\""
|
|
$" property color alertContainer: \"(container-tint $p.surface0 $p.red)\""
|
|
$" property color success: \"($p.green)\""
|
|
$" property color successContainer: \"(container-tint $p.surface0 $p.green)\""
|
|
$" property color warning: \"($p.yellow)\""
|
|
$" property color warningContainer: \"(container-tint $p.surface0 $p.yellow)\""
|
|
$" property color info: \"($info)\""
|
|
$" property color infoContainer: \"(container-tint $p.surface0 $info)\""
|
|
""
|
|
$" property color text: \"($p.text)\""
|
|
$" property color textMuted: \"($p.subtext0)\""
|
|
$" property color textOnAccent: \"($p.crust)\""
|
|
""
|
|
$" property color border: \"($p.crust)\""
|
|
]
|
|
|
|
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 swatches = (dominant-colors $wallpaper_path 8)
|
|
let top = ($swatches | first)
|
|
let flavor = (pick-flavor { r: $top.r, g: $top.g, b: $top.b })
|
|
let accent_names = (pick-accents $flavor $swatches 3 40.0)
|
|
|
|
write-theme-colors $flavor $accent_names
|
|
propagate $flavor
|
|
}
|