sparkline gradient colors improvements

This commit is contained in:
sqshq 2019-04-01 00:35:55 -04:00
parent df18d0fb89
commit 061d77c76a
4 changed files with 14 additions and 16 deletions

View File

@ -19,7 +19,7 @@ type SparkLine struct {
maxValue float64 maxValue float64
minValue float64 minValue float64
scale int scale int
color ui.Color gradient []ui.Color
palette console.Palette palette console.Palette
} }
@ -30,7 +30,7 @@ func NewSparkLine(c config.SparkLineConfig, palette console.Palette) *SparkLine
Consumer: data.NewConsumer(), Consumer: data.NewConsumer(),
values: []float64{}, values: []float64{},
scale: *c.Scale, scale: *c.Scale,
color: *c.Item.Color, gradient: *c.Gradient,
palette: palette, palette: palette,
} }
@ -112,7 +112,7 @@ func (s *SparkLine) Draw(buffer *ui.Buffer) {
} }
for j := 1; j <= top; j++ { for j := 1; j <= top; j++ {
buffer.SetCell(ui.NewCell(console.SymbolVerticalBar, ui.NewStyle(s.palette.GetGradientColor(j-1, height))), image.Pt(s.Inner.Max.X-n-indent, s.Inner.Max.Y-j)) buffer.SetCell(ui.NewCell(console.SymbolVerticalBar, ui.NewStyle(console.GetGradientColor(s.gradient, j-1, height))), image.Pt(s.Inner.Max.X-n-indent, s.Inner.Max.Y-j))
if i == len(s.values)-1 && j == top { if i == len(s.values)-1 && j == top {
buffer.SetString(curValue, textStyle, image.Pt(s.Inner.Max.X-n-indent+2, s.Inner.Max.Y-j)) buffer.SetString(curValue, textStyle, image.Pt(s.Inner.Max.X-n-indent+2, s.Inner.Max.Y-j))
buffer.SetString(minValue, textStyle, image.Pt(s.Inner.Max.X-n-indent+2, s.Max.Y-2)) buffer.SetString(minValue, textStyle, image.Pt(s.Inner.Max.X-n-indent+2, s.Max.Y-2))

View File

@ -57,6 +57,7 @@ type SparkLineConfig struct {
ComponentConfig `yaml:",inline"` ComponentConfig `yaml:",inline"`
Scale *int `yaml:"scale,omitempty"` Scale *int `yaml:"scale,omitempty"`
Item Item `yaml:",inline"` Item Item `yaml:",inline"`
Gradient *[]ui.Color `yaml:",omitempty"`
} }
type BarChartConfig struct { type BarChartConfig struct {

View File

@ -173,11 +173,9 @@ func (c *Config) setDefaultColors() {
} }
for i, s := range c.SparkLines { for i, s := range c.SparkLines {
if s.Item.Color == nil { s.Gradient = &palette.GradientColors[i%(len(palette.GradientColors))]
s.Item.Color = &palette.ContentColors[i%colorsCount]
c.SparkLines[i] = s c.SparkLines[i] = s
} }
}
for i, g := range c.Gauges { for i, g := range c.Gauges {
if g.Color == nil { if g.Color == nil {

View File

@ -37,7 +37,7 @@ const (
type Palette struct { type Palette struct {
ContentColors []ui.Color ContentColors []ui.Color
GradientColors []ui.Color GradientColors [][]ui.Color
BaseColor ui.Color BaseColor ui.Color
MediumColor ui.Color MediumColor ui.Color
ReverseColor ui.Color ReverseColor ui.Color
@ -48,7 +48,7 @@ func GetPalette(theme Theme) Palette {
case ThemeDark: case ThemeDark:
return Palette{ return Palette{
ContentColors: []ui.Color{ColorOlive, ColorDeepSkyBlue, ColorDeepPink, ColorWhite, ColorGrey, ColorGreen, ColorOrange, ColorCian, ColorPurple}, ContentColors: []ui.Color{ColorOlive, ColorDeepSkyBlue, ColorDeepPink, ColorWhite, ColorGrey, ColorGreen, ColorOrange, ColorCian, ColorPurple},
GradientColors: []ui.Color{39, 44, 47, 82, 148, 185, 209, 203, 198, 196, 125}, GradientColors: [][]ui.Color{{39, 33, 62, 93, 164, 161}, {95, 138, 180, 179, 178, 178}},
BaseColor: ColorWhite, BaseColor: ColorWhite,
MediumColor: ColorDarkGrey, MediumColor: ColorDarkGrey,
ReverseColor: ColorBlack, ReverseColor: ColorBlack,
@ -56,7 +56,7 @@ func GetPalette(theme Theme) Palette {
case ThemeLight: case ThemeLight:
return Palette{ return Palette{
ContentColors: []ui.Color{ColorBlack, ColorDarkRed, ColorBlueViolet, ColorGrey, ColorGreen}, ContentColors: []ui.Color{ColorBlack, ColorDarkRed, ColorBlueViolet, ColorGrey, ColorGreen},
GradientColors: []ui.Color{250, 248, 246, 244, 242, 240, 238, 236, 234, 232, 16}, GradientColors: [][]ui.Color{{250, 248, 246, 244, 242, 240, 238, 236, 234, 232, 16}},
BaseColor: ColorBlack, BaseColor: ColorBlack,
MediumColor: ColorLightGrey, MediumColor: ColorLightGrey,
ReverseColor: ColorWhite, ReverseColor: ColorWhite,
@ -66,8 +66,7 @@ func GetPalette(theme Theme) Palette {
} }
} }
// selects gradient color for cur item (out of max items) func GetGradientColor(gradient []ui.Color, cur int, max int) ui.Color {
func (palette *Palette) GetGradientColor(cur int, max int) ui.Color { ratio := float64(len(gradient)) / float64(max)
ratio := float64(len(palette.GradientColors)) / float64(max) return gradient[int(ratio*float64(cur))]
return palette.GradientColors[int(ratio*float64(cur))]
} }