1
0
Fork 0
mirror of https://github.com/Luzifer/streamdeck.git synced 2024-12-29 22:21:24 +00:00

Add background_color attribute to text display elements

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-08-14 15:56:07 +02:00
parent 71716320c5
commit 954a9e9cbc
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
3 changed files with 69 additions and 34 deletions

View file

@ -20,6 +20,7 @@ func init() {
type attributeCollection struct { type attributeCollection struct {
AttachStderr bool `json:"attach_stderr,omitempty" yaml:"attach_stderr,omitempty"` AttachStderr bool `json:"attach_stderr,omitempty" yaml:"attach_stderr,omitempty"`
AttachStdout bool `json:"attach_stdout,omitempty" yaml:"attach_stdout,omitempty"` AttachStdout bool `json:"attach_stdout,omitempty" yaml:"attach_stdout,omitempty"`
BackgroundColor []int `json:"background_color,omitempty" yaml:"background_color,omitempty"`
Border *int `json:"border,omitempty" yaml:"border,omitempty"` Border *int `json:"border,omitempty" yaml:"border,omitempty"`
Caption string `json:"caption,omitempty" yaml:"caption,omitempty"` Caption string `json:"caption,omitempty" yaml:"caption,omitempty"`
ChangeVolume *float64 `json:"change_volume,omitempty" yaml:"change_volume,omitempty"` ChangeVolume *float64 `json:"change_volume,omitempty" yaml:"change_volume,omitempty"`
@ -49,6 +50,10 @@ type attributeCollection struct {
Wait bool `json:"wait,omitempty" yaml:"wait,omitempty"` Wait bool `json:"wait,omitempty" yaml:"wait,omitempty"`
} }
func (a attributeCollection) BackgroundToColor() color.RGBA {
return a.rgbaToColor(a.BackgroundColor)
}
func (a attributeCollection) Clone() attributeCollection { func (a attributeCollection) Clone() attributeCollection {
var ( var (
buf = new(bytes.Buffer) buf = new(bytes.Buffer)
@ -62,10 +67,14 @@ func (a attributeCollection) Clone() attributeCollection {
} }
func (a attributeCollection) RGBAToColor() color.RGBA { func (a attributeCollection) RGBAToColor() color.RGBA {
if len(a.RGBA) != 4 { return a.rgbaToColor(a.RGBA)
}
func (a attributeCollection) rgbaToColor(src []int) color.RGBA {
if len(src) != 4 {
return color.RGBA{} return color.RGBA{}
} }
return color.RGBA{uint8(a.RGBA[0]), uint8(a.RGBA[1]), uint8(a.RGBA[2]), uint8(a.RGBA[3])} return color.RGBA{uint8(src[0]), uint8(src[1]), uint8(src[2]), uint8(src[3])}
} }
type config struct { type config struct {

View file

@ -2,11 +2,12 @@ package main
import ( import (
"context" "context"
"github.com/pkg/errors"
"image/color" "image/color"
_ "image/jpeg" _ "image/jpeg"
_ "image/png" _ "image/png"
"strings" "strings"
"github.com/pkg/errors"
) )
func init() { func init() {
@ -24,6 +25,19 @@ func (d displayElementText) Display(ctx context.Context, idx int, attributes att
) )
// Initialize background // Initialize background
if attributes.BackgroundColor != nil {
if len(attributes.BackgroundColor) != 4 {
return errors.New("Background color definition needs 4 hex values")
}
if err := ctx.Err(); err != nil {
// Page context was cancelled, do not draw
return err
}
imgRenderer.DrawBackgroundColor(attributes.BackgroundToColor())
}
if attributes.Image != "" { if attributes.Image != "" {
if err = imgRenderer.DrawBackgroundFromFile(attributes.Image); err != nil { if err = imgRenderer.DrawBackgroundFromFile(attributes.Image); err != nil {
return errors.Wrap(err, "Unable to draw background from disk") return errors.Wrap(err, "Unable to draw background from disk")

View file

@ -42,6 +42,18 @@ func (t *textOnImageRenderer) DrawBackground(bgi image.Image) {
draw.Draw(t.img, t.img.Bounds(), bgi, image.ZP, draw.Src) draw.Draw(t.img, t.img.Bounds(), bgi, image.ZP, draw.Src)
} }
func (t *textOnImageRenderer) DrawBackgroundColor(col color.RGBA) {
img := image.NewRGBA(image.Rect(0, 0, sd.IconSize(), sd.IconSize()))
for x := 0; x < sd.IconSize(); x++ {
for y := 0; y < sd.IconSize(); y++ {
img.Set(x, y, col)
}
}
t.DrawBackground(img)
}
func (t *textOnImageRenderer) DrawBackgroundFromFile(filename string) error { func (t *textOnImageRenderer) DrawBackgroundFromFile(filename string) error {
bgi, err := t.getImageFromDisk(filename) bgi, err := t.getImageFromDisk(filename)
if err != nil { if err != nil {
@ -70,7 +82,7 @@ func (t *textOnImageRenderer) DrawBigText(text string, fontSizeHint float64, bor
} }
func (t *textOnImageRenderer) DrawCaptionText(text string) error { func (t *textOnImageRenderer) DrawCaptionText(text string) error {
var fontFile = userConfig.CaptionFont fontFile := userConfig.CaptionFont
if fontFile == "" { if fontFile == "" {
fontFile = userConfig.RenderFont fontFile = userConfig.RenderFont
} }
@ -91,7 +103,7 @@ func (t *textOnImageRenderer) DrawCaptionText(text string) error {
} }
} }
var anchor = textDrawAnchorBottom anchor := textDrawAnchorBottom
if userConfig.CaptionPosition == "top" { if userConfig.CaptionPosition == "top" {
anchor = textDrawAnchorTop anchor = textDrawAnchorTop
} }