@@ -125,3 +125,136 @@ func ThemeBg(theme string) string {
125125 }
126126 return bg.String() // "#rrggbb"
127127 }
128+
129+// Palette holds UI chrome colors derived deterministically from a Chroma theme.
130+// The grayscale ramp is a linear interpolation from bg to fg; the accent is the
131+// most saturated token color in the palette; error comes from the Error token.
132+type Palette struct {
133+ Bg string // Theme background
134+ Fg string // Theme foreground (primary text)
135+ Border string // 10% bg→fg — borders, dividers
136+ LinkBg string // 7% bg→fg — subtle hover background
137+ Dim string // 25% bg→fg — tertiary text, timestamps
138+ Muted string // 45% bg→fg — secondary text, reasoning
139+ Accent string // Most saturated token color
140+ Error string // From chroma Error token, lerped 45% toward fg
141+}
142+
143+// ThemePalette derives a full UI color palette from a Chroma theme name.
144+// Deterministic: same theme → same output. Falls back to sensible defaults
145+// when the theme is missing entries.
146+func ThemePalette(theme string) Palette {
147+ sty := styles.Get(theme)
148+ if sty == nil {
149+ return defaultPalette()
150+ }
151+ entry := sty.Get(chroma.Background)
152+ bg := "#000000"
153+ fg := "#c8c8c8"
154+ if entry.Background.IsSet() {
155+ bg = entry.Background.String()
156+ }
157+ if entry.Colour.IsSet() {
158+ fg = entry.Colour.String()
159+ }
160+
161+ p := Palette{
162+ Bg: bg,
163+ Fg: fg,
164+ Border: lerpHex(bg, fg, 0.10),
165+ LinkBg: lerpHex(bg, fg, 0.07),
166+ Dim: lerpHex(bg, fg, 0.25),
167+ Muted: lerpHex(bg, fg, 0.45),
168+ Accent: pickAccent(sty, fg),
169+ Error: pickError(sty, bg, fg),
170+ }
171+ return p
172+}
173+
174+func defaultPalette() Palette {
175+ return Palette{
176+ Bg: "#000000", Fg: "#c8c8c8",
177+ Border: "#141414", LinkBg: "#0e0e0e",
178+ Dim: "#323232", Muted: "#5a5a5a",
179+ Accent: "#00dfff", Error: "#932e2e",
180+ }
181+}
182+
183+// pickAccent returns the most saturated foreground color across all tokens.
184+func pickAccent(sty *chroma.Style, fallback string) string {
185+ best := fallback
186+ bestSat := 0.0
187+ for tt := chroma.TokenType(0); tt < 2000; tt++ {
188+ e := sty.Get(tt)
189+ if !e.Colour.IsSet() {
190+ continue
191+ }
192+ hex := e.Colour.String()
193+ r, g, b := hexToRGBf(hex)
194+ mx := maxf(r, maxf(g, b))
195+ mn := minf(r, minf(g, b))
196+ if mx == 0 {
197+ continue
198+ }
199+ sat := (mx - mn) / mx
200+ if sat > bestSat {
201+ bestSat = sat
202+ best = hex
203+ }
204+ }
205+ return best
206+}
207+
208+// pickError extracts the Error token color and lerps it 45% toward fg
209+// so it's visible but not garish against the theme background.
210+func pickError(sty *chroma.Style, bg, fg string) string {
211+ e := sty.Get(chroma.Error)
212+ if !e.Colour.IsSet() {
213+ return lerpHex(bg, fg, 0.45) // muted fallback
214+ }
215+ return lerpHex(bg, e.Colour.String(), 0.45)
216+}
217+
218+// lerpHex linearly interpolates between two hex colors at fraction t.
219+func lerpHex(a, b string, t float64) string {
220+ ar, ag, ab := hexToRGBf(a)
221+ br, bg, bb := hexToRGBf(b)
222+ return fmt.Sprintf("#%02x%02x%02x",
223+ clampByte(ar+(br-ar)*t),
224+ clampByte(ag+(bg-ag)*t),
225+ clampByte(ab+(bb-ab)*t),
226+ )
227+}
228+
229+func hexToRGBf(hex string) (float64, float64, float64) {
230+ if len(hex) != 7 || hex[0] != '#' {
231+ return 0, 0, 0
232+ }
233+ return float64(hexByte(hex[1], hex[2])),
234+ float64(hexByte(hex[3], hex[4])),
235+ float64(hexByte(hex[5], hex[6]))
236+}
237+
238+func clampByte(v float64) int {
239+ if v < 0 {
240+ return 0
241+ }
242+ if v > 255 {
243+ return 255
244+ }
245+ return int(v + 0.5)
246+}
247+
248+func maxf(a, b float64) float64 {
249+ if a > b {
250+ return a
251+ }
252+ return b
253+}
254+
255+func minf(a, b float64) float64 {
256+ if a < b {
257+ return a
258+ }
259+ return b
260+}