Refactor 256 color

pull/85/head
rkfg 2 years ago
parent 03266a4d66
commit 8c172646b6

@ -31,11 +31,11 @@ func SprintFunc(c color.Style) func(args ...interface{}) string {
type Option func(*options) type Option func(*options)
type options struct { type options struct {
Bold bool bold bool
Bg bool bg bool
} }
func NewOptions(opts []Option) options { func newOptions(opts []Option) options {
options := options{} options := options{}
for i := range opts { for i := range opts {
if opts[i] == nil { if opts[i] == nil {
@ -46,24 +46,24 @@ func NewOptions(opts []Option) options {
return options return options
} }
func Bold(o *options) { o.Bold = true } func Bold(o *options) { o.bold = true }
func Background(o *options) { o.Bg = true } func Background(o *options) { o.bg = true }
func Yellow(opts ...Option) func(a ...interface{}) string { func Yellow(opts ...Option) func(a ...interface{}) string {
options := NewOptions(opts) options := newOptions(opts)
if options.Bold { if options.bold {
return yellowBold return yellowBold
} }
return yellow return yellow
} }
func Green(opts ...Option) func(a ...interface{}) string { func Green(opts ...Option) func(a ...interface{}) string {
options := NewOptions(opts) options := newOptions(opts)
if options.Bold { if options.bold {
return greenBold return greenBold
} }
if options.Bg { if options.bg {
return greenBg return greenBg
} }
@ -71,44 +71,63 @@ func Green(opts ...Option) func(a ...interface{}) string {
} }
func Red(opts ...Option) func(a ...interface{}) string { func Red(opts ...Option) func(a ...interface{}) string {
options := NewOptions(opts) options := newOptions(opts)
if options.Bold { if options.bold {
return redBold return redBold
} }
return red return red
} }
func White(opts ...Option) func(a ...interface{}) string { func White(opts ...Option) func(a ...interface{}) string {
options := NewOptions(opts) options := newOptions(opts)
if options.Bold { if options.bold {
return whiteBold return whiteBold
} }
return white return white
} }
func Cyan(opts ...Option) func(a ...interface{}) string { func Cyan(opts ...Option) func(a ...interface{}) string {
options := NewOptions(opts) options := newOptions(opts)
if options.Bold { if options.bold {
return cyanBold return cyanBold
} }
if options.Bg { if options.bg {
return cyanBg return cyanBg
} }
return cyan return cyan
} }
func Black(opts ...Option) func(a ...interface{}) string { func Black(opts ...Option) func(a ...interface{}) string {
options := NewOptions(opts) options := newOptions(opts)
if options.Bg { if options.bg {
return blackBg return blackBg
} }
return black return black
} }
func Magenta(opts ...Option) func(a ...interface{}) string { func Magenta(opts ...Option) func(a ...interface{}) string {
options := NewOptions(opts) options := newOptions(opts)
if options.Bg { if options.bg {
return magentaBg return magentaBg
} }
return magentaBg return magentaBg
} }
func HSL256(h, s, l float64, opts ...Option) func(a ...interface{}) string {
options := newOptions(opts)
val := color.HSL(h, s, l).C256().Value()
c := color.S256(val)
if options.bg {
fg := color.White.C256().Value()
if l > 0.5 {
fg = color.Black.C256().Value()
}
c = color.S256(fg, val)
}
if options.bold {
c.AddOpts(color.Bold)
}
return func(a ...interface{}) string {
return c.Sprint(a...)
}
}

@ -7,10 +7,9 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/edouardparis/lntop/config" "github.com/edouardparis/lntop/config"
lntcolor "github.com/edouardparis/lntop/ui/color" "github.com/edouardparis/lntop/ui/color"
"github.com/edouardparis/lntop/ui/cursor" "github.com/edouardparis/lntop/ui/cursor"
"github.com/edouardparis/lntop/ui/models" "github.com/edouardparis/lntop/ui/models"
"github.com/gookit/color"
) )
type View interface { type View interface {
@ -139,7 +138,7 @@ func interp(a, b [3]float64, r float64) (result [3]float64) {
return return
} }
func ColorizeAge(age uint32, text string, opts ...lntcolor.Option) string { func ColorizeAge(age uint32, text string, opts ...color.Option) string {
ageColors := [][3]float64{ ageColors := [][3]float64{
{120, 0.9, 0.9}, {120, 0.9, 0.9},
{60, 0.9, 0.6}, {60, 0.9, 0.6},
@ -153,11 +152,5 @@ func ColorizeAge(age uint32, text string, opts ...lntcolor.Option) string {
} else { } else {
cur = ageColors[2] cur = ageColors[2]
} }
val := color.HSL(cur[0]/360, cur[1], cur[2]).C256().Value() return color.HSL256(cur[0]/360, cur[1], cur[2], opts...)(text)
c := color.S256(val)
options := lntcolor.NewOptions(opts)
if options.Bold {
c.AddOpts(color.Bold)
}
return c.Sprint(text)
} }

Loading…
Cancel
Save