Light terminal themes: use the terminal's text color too

On a light background (Catppuccin Latte in Ghostty on macOS) the panes
kept palette color 0, a dark grey slab with white text. The probe now also
asks for the text color (OSC 10). Light backgrounds are used as well, with
text, borders and titles in the terminal's text color, input fields and
buttons in a light shade of it, and the active tab drawn in it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019reK4DUGYqDUjniWYJvwUW
This commit is contained in:
2026-10-21 11:59:44 +02:00
co-authored by Claude Opus 5.5
parent d2e868293f
commit 08d3417b05
6 changed files with 114 additions and 50 deletions
+73 -34
View File
@@ -19,6 +19,8 @@ type terminalInfo struct {
graphics bool // the Kitty graphics protocol is supported
bg color.RGBA // its background color, if hasBG
hasBG bool
fg color.RGBA // its text color, if hasFG
hasFG bool
}
var (
@@ -37,52 +39,89 @@ var terminalAnswers = func() terminalInfo {
// parseTerminalAnswers reads the answers to probeTerminal's questions.
func parseTerminalAnswers(got []byte) (info terminalInfo) {
info.graphics = bytes.Contains(got, []byte("_Gi=31;OK"))
// OSC 11 answer: ESC ] 11 ; rgb:RRRR/GGGG/BBBB, ended by ST or BEL. Each
// component has 1 to 4 hex digits.
if i := bytes.Index(got, []byte("\x1b]11;rgb:")); i >= 0 {
rest := got[i+len("\x1b]11;rgb:"):]
if end := bytes.IndexAny(rest, "\x1b\a"); end >= 0 {
rest = rest[:end]
}
if parts := strings.Split(string(rest), "/"); len(parts) == 3 {
var c [3]uint8
ok := true
for j, p := range parts {
v, err := strconv.ParseUint(p, 16, 16)
if err != nil || len(p) < 1 || len(p) > 4 {
ok = false
break
}
c[j] = uint8(v * 255 / (1<<(4*len(p)) - 1))
}
if ok {
info.bg, info.hasBG = color.RGBA{c[0], c[1], c[2], 255}, true
}
}
}
info.fg, info.hasFG = parseColorAnswer(got, "10")
info.bg, info.hasBG = parseColorAnswer(got, "11")
return info
}
// parseColorAnswer finds the answer to an OSC 10 (text) or 11 (background)
// color query: ESC ] 11 ; rgb:RRRR/GGGG/BBBB, ended by ST or BEL. Each
// component has 1 to 4 hex digits.
func parseColorAnswer(got []byte, osc string) (color.RGBA, bool) {
prefix := []byte("\x1b]" + osc + ";rgb:")
i := bytes.Index(got, prefix)
if i < 0 {
return color.RGBA{}, false
}
rest := got[i+len(prefix):]
if end := bytes.IndexAny(rest, "\x1b\a"); end >= 0 {
rest = rest[:end]
}
parts := strings.Split(string(rest), "/")
if len(parts) != 3 {
return color.RGBA{}, false
}
var c [3]uint8
for j, p := range parts {
v, err := strconv.ParseUint(p, 16, 16)
if err != nil || len(p) < 1 || len(p) > 4 {
return color.RGBA{}, false
}
c[j] = uint8(v * 255 / (1<<(4*len(p)) - 1))
}
return color.RGBA{c[0], c[1], c[2], 255}, true
}
// activeTabTag colors the name of the open view in the tab bar.
var activeTabTag = "[black:white]"
// useTerminalBackground makes the terminal's own background color the
// background of every pane, instead of color 0 of the palette ("black"), which
// color themes often set to a grey that differs from their background (and
// on which grey text almost disappears). Only for dark backgrounds: the text
// colors are made for those. Needs true color, else the exact color cannot be
// drawn. Must run before any widget is created, they copy the style. It
// returns a line for the log that says what was decided and why.
// on which grey text almost disappears; in light themes color 0 is a dark
// grey). On a light background the text, borders and fields switch to the
// terminal's text color and shades between the two, since tview's defaults
// (white text, blue fields) are made for dark ones. The accent colors (yellow,
// green, red, grey) stay palette colors, which a theme picks to suit its
// background. Needs true color, else the exact colors cannot be drawn. Must
// run before any widget is created, they copy the styles. It returns a line
// for the log that says what was decided and why.
func useTerminalBackground(info terminalInfo, env func(string) string) string {
if !info.hasBG {
return "Background: palette color 0 (the terminal did not report its background color)"
}
hex := fmt.Sprintf("#%02x%02x%02x", info.bg.R, info.bg.G, info.bg.B)
switch {
case !hasTrueColor(env):
hex := hexColor(info.bg)
if !hasTrueColor(env) {
return "Background: palette color 0 (the terminal's " + hex + " needs 24-bit color, which " + env("TERM") + " does not have; COLORTERM=truecolor enables it)"
case luminance(info.bg) > 0.4:
return "Background: palette color 0 (the terminal's " + hex + " is light; the text colors are made for dark backgrounds)"
}
tview.Styles.PrimitiveBackgroundColor = tcell.NewRGBColor(int32(info.bg.R), int32(info.bg.G), int32(info.bg.B))
return "Background: the terminal's own color " + hex
st := &tview.Styles
st.PrimitiveBackgroundColor = rgb(info.bg)
if luminance(info.bg) <= 0.5 {
return "Background: the terminal's own color " + hex
}
fg := color.RGBA{0, 0, 0, 255}
if info.hasFG && luminance(info.fg) < luminance(info.bg) {
fg = info.fg
}
st.PrimaryTextColor, st.BorderColor, st.TitleColor, st.GraphicsColor = rgb(fg), rgb(fg), rgb(fg), rgb(fg)
st.ContrastBackgroundColor = rgb(mix(info.bg, fg, 0.12)) // input fields, buttons
st.ContrastSecondaryTextColor = rgb(mix(info.bg, fg, 0.55)) // placeholders, disabled buttons
st.InverseTextColor = rgb(info.bg) // a pressed button: background on text color
activeTabTag = "[" + hexColor(info.bg) + ":" + hexColor(fg) + "]"
return "Background: the terminal's own color " + hex + ", light, with its text color " + hexColor(fg)
}
func rgb(c color.RGBA) tcell.Color {
return tcell.NewRGBColor(int32(c.R), int32(c.G), int32(c.B))
}
func hexColor(c color.RGBA) string { return fmt.Sprintf("#%02x%02x%02x", c.R, c.G, c.B) }
// mix returns the color a fraction t of the way from a to b.
func mix(a, b color.RGBA, t float64) color.RGBA {
m := func(x, y uint8) uint8 { return uint8(float64(x) + (float64(y)-float64(x))*t + 0.5) }
return color.RGBA{m(a.R, b.R), m(a.G, b.G), m(a.B, b.B), 255}
}
// hasTrueColor reports whether tcell will draw 24-bit colors, by the same