lipgloss/color.go

77 lines
1.7 KiB
Go
Raw Normal View History

package lipgloss
import (
"github.com/muesli/termenv"
)
var (
ColorProfile termenv.Profile = termenv.ColorProfile()
color func(string) termenv.Color = ColorProfile.Color
hasDarkBackground bool = termenv.HasDarkBackground()
)
2021-03-31 05:19:38 +03:00
// TerminalColor is a color intended to be rendered in the terminal.
type TerminalColor interface {
value() string
color() termenv.Color
}
// NoColor is used to specify the absence of color styling. When this is active
// foreground colors will be rendered with the terminal's default text color,
// and background colors will not be drawn at all.
//
// Example usage:
//
2021-03-18 21:34:39 +03:00
// var style = someStyle.Copy().Background(lipgloss.NoColor{})
//
2021-03-18 21:34:39 +03:00
type NoColor struct{}
2021-03-18 21:34:39 +03:00
func (n NoColor) value() string {
return ""
}
func (n NoColor) color() termenv.Color {
return color("")
}
2021-03-18 21:34:39 +03:00
var noColor = NoColor{}
// Color specifies a color by hex or ANSI value. For example:
//
2021-03-18 21:34:39 +03:00
// ansiColor := lipgloss.Color("21")
// hexColor := lipgloss.Color("#0000ff")
//
type Color string
func (c Color) value() string {
return string(c)
}
func (c Color) color() termenv.Color {
return color(string(c))
}
// AdaptiveColor provides color options for light and dark backgrounds. The
// appropriate color with be returned based on the darkness of the terminal
// background color determined at runtime.
//
// Example usage:
//
2021-03-18 21:34:39 +03:00
// color := lipgloss.AdaptiveColor{Light: "#0000ff", Dark: "#000099"}
//
type AdaptiveColor struct {
Light string
Dark string
}
func (a AdaptiveColor) value() string {
if hasDarkBackground {
return a.Dark
}
return a.Light
}
func (a AdaptiveColor) color() termenv.Color {
return color(a.value())
}