Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/opencode/src/cli/cmd/tui/context/theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ function resolveTheme(theme: ThemeJson, mode: "dark" | "light") {
}),
) as Partial<ThemeColors>

const textBg = resolved.backgroundElement?.a === 0 ? resolved.background : resolved.backgroundElement
if (resolved.text && textBg) {
resolved.text = contrastRatio(resolved.text, textBg) >= 2 ? resolved.text : readableTextColor(textBg)
}

// Handle selectedListItemText separately since it's optional
const hasSelectedListItemText = theme.theme.selectedListItemText !== undefined
if (hasSelectedListItemText) {
Expand Down Expand Up @@ -231,6 +236,25 @@ function resolveTheme(theme: ThemeJson, mode: "dark" | "light") {
} as Theme
}

function readableTextColor(bg: RGBA) {
const black = RGBA.fromInts(0, 0, 0)
const white = RGBA.fromInts(255, 255, 255)
return contrastRatio(black, bg) >= contrastRatio(white, bg) ? black : white
}

function contrastRatio(a: RGBA, b: RGBA) {
const x = relativeLuminance(a)
const y = relativeLuminance(b)
const max = Math.max(x, y)
const min = Math.min(x, y)
return (max + 0.05) / (min + 0.05)
}

function relativeLuminance(c: RGBA) {
const convert = (v: number) => (v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4)
return 0.2126 * convert(c.r) + 0.7152 * convert(c.g) + 0.0722 * convert(c.b)
}

function ansiToRgba(code: number): RGBA {
// Standard ANSI colors (0-15)
if (code < 16) {
Expand Down
Loading