forge
Explore
Log in

Don't you dare go Hollow

dotfiles

Clone
★ 0

0 watching0 forksShellGPL-3.0334.8 KiBb1320e9Tahinli16 days ago105 commits

theme_colors.sh137 lines, 5.0 KiBHistoryRawDownload
1#!/bin/bash
2# Post-process wallust-generated theme files:
3# - GTK 3/4: resolve mix(#hex, #hex, ratio) to pre-computed hex values
4# - QT 5/6: convert colors and compute mixed surface colors
5
6# Configure ~/.gtkrc-2.0:
7# - Force gtk-theme-name to Raleigh (minimal, no pixmap engine, lets our colors through)
8# - Ensure our include is parsed LAST so our styles win
9#
10# nwg-look owns this file when it is installed, but GTK2 will not read our
11# generated .mine without the include, and on a fresh machine (or without
12# nwg-look) the file does not exist at all — so create it instead of bailing out.
13GTKRC="$HOME/.gtkrc-2.0"
14MINE_INC="include \"$HOME/.gtkrc-2.0.mine\""
15[ -f "$GTKRC" ] || : > "$GTKRC"
16grep -q '^gtk-theme-name=' "$GTKRC" || echo 'gtk-theme-name="Raleigh"' >> "$GTKRC"
17grep -vF "$MINE_INC" "$GTKRC" | sed 's/^gtk-theme-name=.*/gtk-theme-name="Raleigh"/' > "$GTKRC.tmp"
18echo "$MINE_INC" >> "$GTKRC.tmp"
19mv "$GTKRC.tmp" "$GTKRC"
20
21# Convert hex colors to KDE's R,G,B integer format in kdeglobals
22KDEGLOBALS="$HOME/.config/kdeglobals"
23if [ -f "$KDEGLOBALS" ]; then
24 python3 -c "
25import re
26def mix(c1, c2, f):
27 r = int(int(c1[1:3],16)*(1-f) + int(c2[1:3],16)*f)
28 g = int(int(c1[3:5],16)*(1-f) + int(c2[3:5],16)*f)
29 b = int(int(c1[5:7],16)*(1-f) + int(c2[5:7],16)*f)
30 return f'#{r:02x}{g:02x}{b:02x}'
31with open('$KDEGLOBALS') as f:
32 content = f.read()
33content = re.sub(
34 r'mix\(\s*(#[0-9A-Fa-f]{6})\s*,\s*(#[0-9A-Fa-f]{6})\s*,\s*([0-9.]+)\s*\)',
35 lambda m: mix(m.group(1), m.group(2), float(m.group(3))),
36 content
37)
38content = re.sub(
39 r'#([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})',
40 lambda m: f'{int(m.group(1),16)},{int(m.group(2),16)},{int(m.group(3),16)}',
41 content
42)
43with open('$KDEGLOBALS', 'w') as f:
44 f.write(content)
45"
46fi
47
48# Resolve mix() in GTK CSS / gtkrc files, and in the Qt color schemes.
49# The Qt confs must be handled here, before the #RRGGBB -> #ffRRGGBB pass below,
50# because the mix() regex only matches 6-digit hex.
51for css in ~/.config/gtk-4.0/gtk.css ~/.config/gtk-4.0/gtk-dark.css ~/.config/gtk-3.0/gtk.css ~/.gtkrc-2.0.mine ~/.config/qt5ct/colors/wallust.conf ~/.config/qt6ct/colors/wallust.conf; do
52 [ -f "$css" ] || continue
53 python3 -c "
54import re
55def mix(c1, c2, f):
56 r = int(int(c1[1:3],16)*(1-f) + int(c2[1:3],16)*f)
57 g = int(int(c1[3:5],16)*(1-f) + int(c2[3:5],16)*f)
58 b = int(int(c1[5:7],16)*(1-f) + int(c2[5:7],16)*f)
59 return f'#{r:02x}{g:02x}{b:02x}'
60
61with open('$css') as f:
62 content = f.read()
63content = re.sub(
64 r'mix\(\s*(#[0-9A-Fa-f]{6})\s*,\s*(#[0-9A-Fa-f]{6})\s*,\s*([0-9.]+)\s*\)',
65 lambda m: mix(m.group(1), m.group(2), float(m.group(3))),
66 content
67)
68with open('$css', 'w') as f:
69 f.write(content)
70"
71done
72
73for conf in ~/.config/qt5ct/colors/wallust.conf ~/.config/qt6ct/colors/wallust.conf; do
74 [ -f "$conf" ] || continue
75
76 # Convert #RRGGBB to #ffRRGGBB (strip any existing ff first to avoid double-prefix)
77 sed -i 's/#ff\([0-9A-Fa-f]\{6\}\)/#\1/g; s/#\([0-9A-Fa-f]\{6\}\)/#ff\1/g' "$conf"
78
79 # Extract foreground (pos 0), color1 (pos 1), background (pos 4), color4 (pos 12)
80 IFS=',' read -ra COLS <<< "$(grep '^active_colors=' "$conf" | cut -d'=' -f2)"
81 FG="${COLS[0]// /}"
82 C1="${COLS[1]// /}"
83 BG="${COLS[4]// /}"
84 C4="${COLS[12]// /}"
85
86 FG="${FG/#\#ff/}"
87 C1="${C1/#\#ff/}"
88 BG="${BG/#\#ff/}"
89 C4="${C4/#\#ff/}"
90
91 # Compute mixed colors:
92 # surface = mix(bg, c1, 0.35) - window/base
93 # button = mix(bg, c1, 0.45) - button bg
94 # accent = mix(c4, fg, 0.3) - lightened selection
95 COMPUTED=$(python3 -c "
96def m(a, b, f):
97 r = int(int(a[0:2],16)*(1-f) + int(b[0:2],16)*f)
98 g = int(int(a[2:4],16)*(1-f) + int(b[2:4],16)*f)
99 bl = int(int(a[4:6],16)*(1-f) + int(b[4:6],16)*f)
100 return f'{r:02X}{g:02X}{bl:02X}'
101print(m('$BG', '$C1', 0.35))
102print(m('$BG', '$C1', 0.45))
103print(m('$C4', '$FG', 0.3))
104")
105 MIXED=$(echo "$COMPUTED" | sed -n '1p')
106 MIXED_BTN=$(echo "$COMPUTED" | sed -n '2p')
107 MIXED_ACCENT=$(echo "$COMPUTED" | sed -n '3p')
108
109 # Replace surface roles: Base(9), Window(10), AlternateBase(16), ToolTipBase(18) with MIXED
110 # NOTE: role order is QPalette::ColorRole — index 17 is NoRole, ToolTipBase is 18.
111 # Replace Button(1) with MIXED_BTN
112 # Replace Highlight(12) and pos 20 with MIXED_ACCENT
113 python3 -c "
114mixed = '#ff$MIXED'
115mixed_btn = '#ff$MIXED_BTN'
116mixed_accent = '#ff$MIXED_ACCENT'
117c1 = '#ff$C1'
118c4 = '#ff$C4'
119with open('$conf') as f: lines = f.readlines()
120out = []
121for line in lines:
122 if '=' not in line or line.startswith('['):
123 out.append(line); continue
124 key, vals = line.strip().split('=', 1)
125 cols = [v.strip() for v in vals.split(',')]
126 for i in [9, 10, 16, 18]:
127 if i < len(cols) and cols[i] == c1:
128 cols[i] = mixed
129 if 1 < len(cols) and cols[1] == c1:
130 cols[1] = mixed_btn
131 for i in [12, 20]:
132 if i < len(cols) and cols[i] == c4:
133 cols[i] = mixed_accent
134 out.append(key + '=' + ', '.join(cols) + '\n')
135with open('$conf', 'w') as f: f.writelines(out)
136"
137done