Files
skillshare/internal/theme/contrast.go
T
Willie 36e22477fe feat(theme): add internal/theme package with palettes and detection
Introduces a unified light/dark terminal theme system:
- Theme struct with sync.Once-backed Get()/Reset() singleton
- darkPalette (softened Primary from 15 to 252) and lightPalette
  (new, WCAG AA compliant against white backgrounds)
- Style constructors (Primary, Muted, Accent, Success, Warning,
  Danger, Info, Title, SelectedRow, Badge, Severity)
- ANSISet for plain CLI output
- Resolution precedence: NO_COLOR > SKILLSHARE_THEME > OSC 11 probe
  > dark fallback
- 300ms timeout on OSC 11 probe with documented goroutine leak tradeoff
- canDetect() gate for non-TTY, CI, TERM=dumb, Windows-safe
- WCAG contrast tests as regression guard for future palette edits

NO_COLOR env var follows the no-color.org spec: only activates when
set AND non-empty (empty string does not enable no-color mode).

This commit adds the package but does not migrate any existing
callers. The next commits migrate list TUIs (fixes #125), plain CLI
output, and remaining TUIs.

Refs: #125
2026-04-10 14:26:44 +08:00

105 lines
2.8 KiB
Go

package theme
import (
"math"
"strconv"
"strings"
"github.com/charmbracelet/lipgloss"
)
// contrastRatio returns the WCAG contrast ratio between fg and bg.
// Both colors may be 256-palette indices (e.g. "252") or hex strings
// (e.g. "#FFFFFF"). Returns 1.0 on parse failure, which is the minimum
// possible ratio (identical colors).
func contrastRatio(fg, bg lipgloss.Color) float64 {
fgLum, ok1 := relativeLuminance(string(fg))
bgLum, ok2 := relativeLuminance(string(bg))
if !ok1 || !ok2 {
return 1.0
}
lighter, darker := fgLum, bgLum
if darker > lighter {
lighter, darker = darker, lighter
}
return (lighter + 0.05) / (darker + 0.05)
}
// relativeLuminance computes the WCAG relative luminance (0..1) of a color
// given either as a 256-palette index string or a hex string.
func relativeLuminance(s string) (float64, bool) {
r, g, b, ok := parseColorRGB(s)
if !ok {
return 0, false
}
// WCAG formula: https://www.w3.org/WAI/GL/wiki/Relative_luminance
return 0.2126*channelLum(r) + 0.7152*channelLum(g) + 0.0722*channelLum(b), true
}
// channelLum converts an 8-bit channel value to a WCAG linearized component.
func channelLum(c int) float64 {
cs := float64(c) / 255.0
if cs <= 0.03928 {
return cs / 12.92
}
return math.Pow((cs+0.055)/1.055, 2.4)
}
// parseColorRGB converts either "#RRGGBB" or a 256-palette index like "232"
// into 8-bit RGB channels.
func parseColorRGB(s string) (r, g, b int, ok bool) {
if strings.HasPrefix(s, "#") && len(s) == 7 {
rv, err := strconv.ParseInt(s[1:3], 16, 32)
if err != nil {
return 0, 0, 0, false
}
gv, err := strconv.ParseInt(s[3:5], 16, 32)
if err != nil {
return 0, 0, 0, false
}
bv, err := strconv.ParseInt(s[5:7], 16, 32)
if err != nil {
return 0, 0, 0, false
}
return int(rv), int(gv), int(bv), true
}
// 256-palette index
idx, err := strconv.Atoi(s)
if err != nil || idx < 0 || idx > 255 {
return 0, 0, 0, false
}
return palette256ToRGB(idx)
}
// palette256ToRGB maps an xterm 256-color index to approximate 8-bit RGB.
// Ranges:
//
// 0- 7: standard ANSI (hardcoded)
// 8- 15: bright ANSI (hardcoded)
// 16-231: 6x6x6 color cube
// 232-255: 24-step grayscale
func palette256ToRGB(idx int) (r, g, b int, ok bool) {
standard := [16][3]int{
{0, 0, 0}, {128, 0, 0}, {0, 128, 0}, {128, 128, 0},
{0, 0, 128}, {128, 0, 128}, {0, 128, 128}, {192, 192, 192},
{128, 128, 128}, {255, 0, 0}, {0, 255, 0}, {255, 255, 0},
{0, 0, 255}, {255, 0, 255}, {0, 255, 255}, {255, 255, 255},
}
if idx < 16 {
c := standard[idx]
return c[0], c[1], c[2], true
}
if idx < 232 {
// 6x6x6 cube: colors at 0, 95, 135, 175, 215, 255
levels := [6]int{0, 95, 135, 175, 215, 255}
n := idx - 16
r = levels[(n/36)%6]
g = levels[(n/6)%6]
b = levels[n%6]
return r, g, b, true
}
// Grayscale: 232..255 → 8, 18, 28, ..., 238
v := 8 + (idx-232)*10
return v, v, v, true
}