k9s/internal/config/color.go

62 lines
1.1 KiB
Go

// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of K9s
package config
import (
"fmt"
"github.com/derailed/tcell/v2"
)
const (
// DefaultColor represents a default color.
DefaultColor Color = "default"
// TransparentColor represents the terminal bg color.
TransparentColor Color = "-"
)
// NewColor returns a new color.
func NewColor(c string) Color {
return Color(c)
}
// String returns color as string.
func (c Color) String() string {
if c.isHex() {
return string(c)
}
if c == DefaultColor {
return "-"
}
col := c.Color().TrueColor().Hex()
if col < 0 {
return "-"
}
return fmt.Sprintf("#%06x", col)
}
func (c Color) isHex() bool {
return len(c) == 7 && c[0] == '#'
}
// Color returns a view color.
func (c Color) Color() tcell.Color {
if c == DefaultColor {
return tcell.ColorDefault
}
return tcell.GetColor(string(c)).TrueColor()
}
// Colors converts series string colors to colors.
func (c Colors) Colors() []tcell.Color {
cc := make([]tcell.Color, 0, len(c))
for _, color := range c {
cc = append(cc, color.Color())
}
return cc
}