Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions go/display/tui/style/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,21 @@ type LightDark = lipgloss.LightDarkFunc

// NewLightDark builds a LightDark chooser for the given background.
func NewLightDark(isDark bool) LightDark { return lipgloss.LightDark(isDark) }

// AdaptiveColor pairs a light-background and a dark-background colour for one
// role, resolved once the terminal background is known — the render-time
// replacement for lipgloss v1's implicit adaptation, which v2 removed. A theme
// keeps the pair as data and resolves it per frame, e.g.
//
// accent := style.AdaptiveColor{Light: "#2e5cc5", Dark: "#7aa2f7"}
// st := style.New().Foreground(accent.Resolve(isDark))
type AdaptiveColor struct {
Light, Dark string
}

// Resolve returns the Light Paint on a light terminal and the Dark Paint on a
// dark one, delegating to NewLightDark so it matches every other light/dark
// choice in the package.
func (a AdaptiveColor) Resolve(isDark bool) Paint {
return NewLightDark(isDark)(Color(a.Light), Color(a.Dark))
}
33 changes: 33 additions & 0 deletions go/display/tui/style/color_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package style_test

import (
"testing"

"dappco.re/go/html/display/tui/style"
)

func TestAdaptiveColor_Resolve_PicksDarkOnADarkTerminal(t *testing.T) {
c := style.AdaptiveColor{Light: "#000000", Dark: "#ffffff"}

r, g, b, _ := c.Resolve(true).RGBA()
if r != 0xffff || g != 0xffff || b != 0xffff {
t.Fatalf("Resolve(isDark=true) = %d,%d,%d, want the Dark colour white (65535,65535,65535)", r, g, b)
}
}

func TestAdaptiveColor_Resolve_PicksLightOnALightTerminal(t *testing.T) {
c := style.AdaptiveColor{Light: "#000000", Dark: "#ffffff"}

r, g, b, _ := c.Resolve(false).RGBA()
if r != 0 || g != 0 || b != 0 {
t.Fatalf("Resolve(isDark=false) = %d,%d,%d, want the Light colour black (0,0,0)", r, g, b)
}
}

func TestAdaptiveColor_Resolve_ZeroValueResolvesWithoutPanicking(t *testing.T) {
var c style.AdaptiveColor // both fields empty — an unset theme role

if got := c.Resolve(true); got == nil {

Check warning on line 30 in go/display/tui/style/color_test.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unnecessary variable declaration and use the expression directly in the condition.

See more on https://sonarcloud.io/project/issues?id=dAppCore_go-html&issues=AZ-Pu9rZ5BVFeMbM4MBA&open=AZ-Pu9rZ5BVFeMbM4MBA&pullRequest=34
t.Fatal("Resolve on a zero AdaptiveColor returned a nil Paint, want a non-nil no-colour")
}
}
12 changes: 12 additions & 0 deletions go/display/tui/style/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ func ExampleBlend1D() {
// Output:
// true
}

// ExampleAdaptiveColor_Resolve keeps one light/dark colour pair and resolves it
// for a dark terminal — the per-frame replacement for lipgloss v1's implicit
// AdaptiveColor, which v2 removed.
func ExampleAdaptiveColor_Resolve() {
accent := style.AdaptiveColor{Light: "#000000", Dark: "#ffffff"}

r, g, b, _ := accent.Resolve(true).RGBA()
fmt.Printf("%d %d %d\n", r, g, b)
// Output:
// 65535 65535 65535
}
Loading