Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rename LightDark to Adapt per @bashbunni's acute suggestion #392

Merged
merged 2 commits into from
Oct 18, 2024
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
4 changes: 2 additions & 2 deletions adaptive/adaptive.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
// appropriate color based on the terminal's background color.
// When a program imports this package, it will query the terminal's background
// color and use it to determine whether to use the light or dark color.
var colorFn lipgloss.LightDark
var colorFn lipgloss.Adapt

func init() {
Query()
Expand All @@ -29,7 +29,7 @@ func init() {
// Query queries the terminal's background color and updates the color function
// accordingly.
func Query() {
colorFn = lipgloss.LightDark(func() bool {
colorFn = lipgloss.Adapt(func() bool {
state, err := term.MakeRaw(Stdin.Fd())
if err != nil {
return HasDarkBackground
Expand Down
52 changes: 33 additions & 19 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ func Color(c any) color.Color {
return h
} else if i, err := strconv.Atoi(c); err == nil {
if i < 16 { //nolint:gomnd
return ansi.BasicColor(i)
return ansi.BasicColor(i) //nolint:gosec
} else if i < 256 { //nolint:gomnd
return ansi.ExtendedColor(i)
return ansi.ExtendedColor(i) //nolint:gosec
}
return ansi.TrueColor(i)
return ansi.TrueColor(i) //nolint:gosec
}
return noColor
case int:
if c < 16 {
return ansi.BasicColor(c)
return ansi.BasicColor(c) //nolint:gosec
} else if c < 256 {
return ansi.ExtendedColor(c)
return ansi.ExtendedColor(c) //nolint:gosec
}
return ansi.TrueColor(c)
return ansi.TrueColor(c) //nolint:gosec
case color.Color:
return c
}
Expand Down Expand Up @@ -113,36 +113,50 @@ func (c RGBColor) RGBA() (r, g, b, a uint32) {
// colorB := lipgloss.ANSIColor(134)
type ANSIColor = ansi.ExtendedColor

// LightDark is a helper type that can be used to specify colors that should be
// used based on the terminal's background color.
// Adapt is a simple helper type that can be used to choose the appropriate
// color based on whether the terminal has a light or dark background.
//
// Example usage:
// adaptive := lipgloss.Adapt(hasDarkBackground)
// theRightColor := adaptive.Color("#0000ff", "#ff0000")
//
// In practice, there are slightly different workflows between Bubble Tea and
// Lip Gloss standalone.
//
// In Bubble Tea listen for tea.BackgroundColorMessage, which automatically
// flows through Update on start, and whenever the background color changes:
//
// case tea.BackgroundColorMessage:
// m.hasDarkBackground = msg.IsDark()
//
// Later, when you're rendering:
//
// adaptive := lipgloss.Adapt(m.hasDarkBackground)
// myHotColor := adaptive.Color("#ff0000", "#0000ff")
//
// In standalone Lip Gloss, the workflow is simpler:
//
// useDark := lipgloss.LightDark(true)
// light := "#0000ff"
// dark := "#ff0000"
// colorToUse := useDark.Color(light, dark)
// fmt.Println(colorToUse)
type LightDark bool
// ...
type Adapt bool

// Color returns the color that should be used based on the terminal's
// background color.
func (a LightDark) Color(light, dark any) color.Color {
func (a Adapt) Color(light, dark any) color.Color {
if bool(a) {
return Color(dark)
}
return Color(light)
}

// IsDarkColor returns whether the given color is dark.
// IsDarkColor returns whether the given color is dark (based on the luminance
// portion of the color as interpreted as HSL).
//
// Example usage:
//
// color := lipgloss.Color("#0000ff")
// if lipgloss.IsDarkColor(color) {
// fmt.Println("It's dark!")
// fmt.Println("It's dark! I love darkness!")
// } else {
// fmt.Println("It's light!")
// fmt.Println("It's light! Cover your eyes!")
// }
func IsDarkColor(c color.Color) bool {
col, ok := colorful.MakeColor(c)
Expand Down
Loading