306 lines
6.7 KiB
Go
306 lines
6.7 KiB
Go
package app
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
"image/draw"
|
|
|
|
"github.com/BeatGlow/display/pixel"
|
|
|
|
"github.com/BeatGlow/beatglow/app/font"
|
|
)
|
|
|
|
var (
|
|
// DefaultTheme is the default theme per color model.
|
|
DefaultTheme = &Theme{
|
|
Mono: &defaultMonoThemeStyles,
|
|
Gray: &defaultGrayThemeStyles,
|
|
Color: &defaultColorThemeStyles,
|
|
}
|
|
|
|
// UserTheme can be set to override styles.
|
|
UserTheme *Theme
|
|
)
|
|
|
|
var (
|
|
defaultMonoObjectStyle = ObjectStyle{
|
|
BackgroundColor: pixel.Off,
|
|
ForegroundColor: pixel.On,
|
|
BorderColor: pixel.On,
|
|
}
|
|
defaultMonoThemeStyles = Styles{
|
|
Window: defaultMonoObjectStyle,
|
|
Progress: defaultMonoObjectStyle,
|
|
Image: defaultMonoObjectStyle,
|
|
Graph: defaultMonoObjectStyle,
|
|
Text: TextStyle{
|
|
ObjectStyle: defaultMonoObjectStyle,
|
|
Font: font.SmoochSans,
|
|
},
|
|
}
|
|
|
|
defaultGrayObjectStyle = ObjectStyle{
|
|
Background: image.Black,
|
|
BackgroundColor: color.Black,
|
|
ForegroundColor: color.White,
|
|
BorderColor: color.Gray{Y: 0xBF},
|
|
}
|
|
defaultGrayThemeStyles = Styles{
|
|
Window: defaultGrayObjectStyle,
|
|
Progress: defaultGrayObjectStyle,
|
|
Image: defaultGrayObjectStyle,
|
|
Graph: defaultGrayObjectStyle,
|
|
Text: TextStyle{
|
|
ObjectStyle: defaultGrayObjectStyle, // .WithMargin(1),
|
|
Font: font.Spleen5x8,
|
|
},
|
|
}
|
|
|
|
defaultColorObjectStyle = ObjectStyle{
|
|
BackgroundColor: color.RGBA{R: 0xB0, G: 0x26, B: 0xFF, A: 0xFF},
|
|
ForegroundColor: color.RGBA{R: 0x01, G: 0xFB, B: 0xC1, A: 0xFF},
|
|
BorderColor: color.RGBA{R: 0x1D, G: 0xEA, B: 0xA1, A: 0xFF},
|
|
}
|
|
defaultColorThemeStyles = Styles{
|
|
Window: defaultColorObjectStyle,
|
|
Progress: defaultColorObjectStyle, // .WithBorder(1).Inverse(),
|
|
Image: defaultColorObjectStyle,
|
|
Graph: defaultColorObjectStyle,
|
|
Text: TextStyle{
|
|
ObjectStyle: defaultColorObjectStyle, // .WithMargin(1),
|
|
Font: font.SmoochSans,
|
|
},
|
|
}
|
|
)
|
|
|
|
type Theme struct {
|
|
Mono *Styles
|
|
Gray *Styles
|
|
Color *Styles
|
|
}
|
|
|
|
type Styles struct {
|
|
Window ObjectStyle
|
|
Progress ObjectStyle
|
|
Text TextStyle
|
|
Image ObjectStyle
|
|
Graph ObjectStyle
|
|
}
|
|
|
|
func StylesFor(ctx Context) *Styles {
|
|
theme := UserTheme
|
|
if theme == nil {
|
|
// Use the default theme.
|
|
theme = DefaultTheme
|
|
} else {
|
|
// Make sure all color models have a theme defined.
|
|
if theme.Mono == nil {
|
|
theme.Mono = DefaultTheme.Mono
|
|
}
|
|
if theme.Gray == nil {
|
|
theme.Gray = DefaultTheme.Gray
|
|
}
|
|
if theme.Color == nil {
|
|
theme.Color = DefaultTheme.Color
|
|
}
|
|
}
|
|
|
|
model := ctx.Display().ColorModel()
|
|
switch model {
|
|
case pixel.MonoModel:
|
|
return theme.Mono
|
|
case pixel.Gray2Model, pixel.Gray4Model, color.GrayModel, color.Gray16Model:
|
|
return theme.Gray
|
|
default:
|
|
return theme.Color
|
|
}
|
|
}
|
|
|
|
type ObjectStyle struct {
|
|
// Background image or color.
|
|
Background image.Image
|
|
BackgroundColor color.Color
|
|
|
|
// Foreground image (mask) or color.
|
|
Foreground image.Image
|
|
ForegroundColor color.Color
|
|
|
|
// Margin around the object.
|
|
Margin Edge
|
|
|
|
// Padding inside the object.
|
|
Padding Edge
|
|
|
|
// Border around the object.
|
|
Border Edge
|
|
BorderColor color.Color
|
|
|
|
// Stroke size for lines etc.
|
|
Stroke int
|
|
}
|
|
|
|
func (s ObjectStyle) WithBackgroundColor(c color.Color) ObjectStyle {
|
|
i := new(ObjectStyle)
|
|
*i = s
|
|
i.Background = nil
|
|
i.BackgroundColor = c
|
|
return *i
|
|
}
|
|
|
|
func (s ObjectStyle) WithForegroundColor(c color.Color) ObjectStyle {
|
|
i := new(ObjectStyle)
|
|
*i = s
|
|
i.Foreground = nil
|
|
i.ForegroundColor = c
|
|
return *i
|
|
}
|
|
|
|
func (s ObjectStyle) Inverse() ObjectStyle {
|
|
i := new(ObjectStyle)
|
|
*i = s
|
|
i.Background, i.Foreground = i.Foreground, i.Background
|
|
i.BackgroundColor, i.ForegroundColor = i.ForegroundColor, i.BackgroundColor
|
|
return *i
|
|
}
|
|
|
|
func (s ObjectStyle) WithBorder(size ...int) ObjectStyle {
|
|
i := new(ObjectStyle)
|
|
*i = s
|
|
switch len(size) {
|
|
case 0:
|
|
return s
|
|
case 1:
|
|
i.Border = Edge{size[0], size[0], size[0], size[0]}
|
|
case 2:
|
|
i.Border = Edge{size[0], size[0], size[1], size[1]}
|
|
case 3:
|
|
i.Border = Edge{size[0], size[1], size[2], size[2]}
|
|
default:
|
|
i.Border = Edge{size[0], size[1], size[2], size[3]}
|
|
}
|
|
return *i
|
|
}
|
|
|
|
func (s ObjectStyle) WithMargin(size ...int) ObjectStyle {
|
|
i := new(ObjectStyle)
|
|
*i = s
|
|
switch len(size) {
|
|
case 0:
|
|
return s
|
|
case 1:
|
|
i.Margin = Edge{size[0], size[0], size[0], size[0]}
|
|
case 2:
|
|
i.Margin = Edge{size[0], size[0], size[1], size[1]}
|
|
case 3:
|
|
i.Margin = Edge{size[0], size[1], size[2], size[2]}
|
|
default:
|
|
i.Margin = Edge{size[0], size[1], size[2], size[3]}
|
|
}
|
|
return *i
|
|
}
|
|
|
|
func (s ObjectStyle) WithPadding(size ...int) ObjectStyle {
|
|
i := new(ObjectStyle)
|
|
*i = s
|
|
switch len(size) {
|
|
case 0:
|
|
return s
|
|
case 1:
|
|
i.Padding = Edge{size[0], size[0], size[0], size[0]}
|
|
case 2:
|
|
i.Padding = Edge{size[0], size[0], size[1], size[1]}
|
|
case 3:
|
|
i.Padding = Edge{size[0], size[1], size[2], size[2]}
|
|
default:
|
|
i.Padding = Edge{size[0], size[1], size[2], size[3]}
|
|
}
|
|
return *i
|
|
}
|
|
|
|
// boundedImage translates (x, y) coorindates to coordinats relative to View.Min; if
|
|
// writes are outside of View, they are ignored.
|
|
type boundedImage struct {
|
|
draw.Image
|
|
View image.Rectangle
|
|
}
|
|
|
|
func (i boundedImage) At(x, y int) color.Color {
|
|
if !(image.Point{X: x, Y: y}).In(i.View.Sub(i.View.Min)) {
|
|
return color.Transparent
|
|
}
|
|
return i.Image.At(x+i.View.Min.X, y+i.View.Min.Y)
|
|
}
|
|
|
|
func (i boundedImage) Set(x, y int, c color.Color) {
|
|
if !(image.Point{X: x, Y: y}).In(i.View.Sub(i.View.Min)) {
|
|
return
|
|
}
|
|
i.Image.Set(x+i.View.Min.X, y+i.View.Min.Y, c)
|
|
}
|
|
|
|
func (i boundedImage) Bounds() image.Rectangle {
|
|
return i.View
|
|
}
|
|
|
|
// Outer returns an image that draws into i's outer region (inside the border).
|
|
func (s ObjectStyle) Outer(i draw.Image) draw.Image {
|
|
bounds := i.Bounds()
|
|
bounds.Min.X += s.Margin.Left + s.Border.Left
|
|
bounds.Min.Y += s.Margin.Top + s.Border.Top
|
|
bounds.Max.X -= s.Margin.Right + s.Border.Right
|
|
bounds.Max.Y -= s.Margin.Bottom + s.Border.Bottom
|
|
return boundedImage{i, bounds}
|
|
}
|
|
|
|
// Inner returns an image that draws into i's inner region.
|
|
func (s ObjectStyle) Inner(i draw.Image) draw.Image {
|
|
bounded := s.Outer(i).(boundedImage)
|
|
bounded.View.Min.X += s.Padding.Left
|
|
bounded.View.Min.Y += s.Padding.Top
|
|
bounded.View.Max.X -= s.Padding.Right
|
|
bounded.View.Max.Y -= s.Padding.Bottom
|
|
return bounded
|
|
}
|
|
|
|
type TextStyle struct {
|
|
ObjectStyle
|
|
Font font.Font
|
|
}
|
|
|
|
func (s TextStyle) WithBackgroundColor(c color.Color) TextStyle {
|
|
i := new(TextStyle)
|
|
*i = s
|
|
i.Background = nil
|
|
i.BackgroundColor = c
|
|
return *i
|
|
}
|
|
|
|
func (s TextStyle) WithForegroundColor(c color.Color) TextStyle {
|
|
i := new(TextStyle)
|
|
*i = s
|
|
i.Foreground = nil
|
|
i.ForegroundColor = c
|
|
return *i
|
|
}
|
|
|
|
func (s TextStyle) WithFont(font font.Font) TextStyle {
|
|
return TextStyle{
|
|
ObjectStyle: s.ObjectStyle,
|
|
Font: font,
|
|
}
|
|
}
|
|
|
|
type Edge struct {
|
|
Left int
|
|
Right int
|
|
Top int
|
|
Bottom int
|
|
}
|
|
|
|
func (edge Edge) Size() image.Point {
|
|
return image.Point{
|
|
X: edge.Left + edge.Right,
|
|
Y: edge.Top + edge.Bottom,
|
|
}
|
|
}
|