gomu/colors.go

102 lines
2.1 KiB
Go
Raw Normal View History

2020-08-22 14:41:03 +08:00
package main
import (
2021-03-15 13:26:51 +08:00
"fmt"
2021-01-21 01:30:16 +08:00
"github.com/gdamore/tcell/v2"
2021-03-15 13:26:51 +08:00
"github.com/rivo/tview"
2020-08-22 14:41:03 +08:00
)
type Colors struct {
accent tcell.Color
foreground tcell.Color
background tcell.Color
// title refers to now_playing_title in config file
title tcell.Color
popup tcell.Color
playlist tcell.Color
}
func newColor() *Colors {
defaultColors := map[string]string{
2021-02-18 10:23:15 +08:00
"Color.accent": "#008B8B",
"Color.foreground": "#FFFFFF",
"Color.background": "none",
"Color.popup": "#0A0F14",
"Color.now_playing_title": "#017702",
"Color.playlist": "#008B8B",
2020-08-22 14:41:03 +08:00
}
2021-02-14 13:45:43 +08:00
anko := gomu.anko
2020-08-22 14:41:03 +08:00
// Validate hex color
for k, v := range defaultColors {
// color from the config file
2021-02-14 14:35:36 +08:00
cfgColor := anko.GetString(k)
2020-08-22 14:41:03 +08:00
if validHexColor(cfgColor) {
continue
}
// use default value if invalid hex color was given
2021-02-14 14:35:36 +08:00
anko.Set(k, v)
2020-08-22 14:41:03 +08:00
}
// handle none background color
var bgColor tcell.Color
2021-02-18 10:23:15 +08:00
bg := anko.GetString("Color.background")
2020-08-22 14:41:03 +08:00
if bg == "none" {
bgColor = tcell.ColorDefault
} else {
bgColor = tcell.GetColor(bg)
}
2021-02-18 10:23:15 +08:00
accent := anko.GetString("Color.accent")
foreground := anko.GetString("Color.foreground")
popup := anko.GetString("Color.popup")
title := anko.GetString("Color.now_playing_title")
playlist := anko.GetString("Color.playlist")
2020-08-22 14:41:03 +08:00
color := &Colors{
accent: tcell.GetColor(accent),
foreground: tcell.GetColor(foreground),
2020-08-22 14:41:03 +08:00
background: bgColor,
popup: tcell.GetColor(popup),
title: tcell.GetColor(title),
playlist: tcell.GetColor(playlist),
2020-08-22 14:41:03 +08:00
}
return color
}
2021-03-15 13:26:51 +08:00
func colorsPopup() tview.Primitive {
textView := tview.NewTextView().
SetWrap(true).
SetDynamicColors(true).
SetWrap(true).
SetWordWrap(true)
textView.
SetBorder(true).
SetTitle(" Colors ").
SetBorderPadding(1, 1, 2, 2)
for i := tcell.ColorBlack; i <= tcell.ColorYellowGreen; i++ {
fmt.Fprintf(textView, "%-3d [:#%06x] [-:-] ", i - tcell.ColorBlack, i.Hex())
}
textView.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEsc:
gomu.pages.RemovePage("show-color-popup")
gomu.popups.pop()
}
return event
})
return textView
}