From c2bc9d939aa1b4cdd5f00e72df30f4f122e933ed Mon Sep 17 00:00:00 2001 From: Henrik Danielsson Date: Mon, 11 Sep 2023 22:58:46 +0200 Subject: [PATCH] Allow setting a solid key background and blending with the background. --- README.md | 17 +++++++++++++++-- config.go | 10 ++++++---- deck.go | 32 +++++++++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c8ac30b..42e2654 100644 --- a/README.md +++ b/README.md @@ -371,13 +371,26 @@ pressed: ### Background Image -You can configure each deck to display an individual wallpaper behind its -widgets: +You can configure each deck to display an individual wallpaper behind the entire +deck: ```toml background = "/some/image.png" ``` +A key can override the deck background with a solid color: + +```toml +[[keys]] + index = 1 + backgroundColor = "#ff0000" # Sets the background to solid red. +``` + +The color is by default rendered on top of any deck background image, but if +that image has transparency you can also specify `backgroundMode = "blend"` to +have the background color only show through the transparent image regions. +This background blend or solid color is static a widget is then rendered on top. + ### Re-using another deck's configuration If you specify a `parent` inside a deck's configuration, it will inherit all diff --git a/config.go b/config.go index e21add0..536bcbb 100644 --- a/config.go +++ b/config.go @@ -42,10 +42,12 @@ type WidgetConfig struct { // KeyConfig holds the entire configuration for a single key. type KeyConfig struct { - Index uint8 `toml:"index"` - Widget WidgetConfig `toml:"widget"` - Action *ActionConfig `toml:"action,omitempty"` - ActionHold *ActionConfig `toml:"action_hold,omitempty"` + Index uint8 `toml:"index"` + Widget WidgetConfig `toml:"widget"` + Action *ActionConfig `toml:"action,omitempty"` + ActionHold *ActionConfig `toml:"action_hold,omitempty"` + BackgroundColor string `toml:"backgroundColor,omitempty"` + BackgroundMode string `toml:"backgroundMode,omitempty"` } // Keys is a slice of keys. diff --git a/deck.go b/deck.go index a260afb..059c0dd 100644 --- a/deck.go +++ b/deck.go @@ -3,6 +3,7 @@ package main import ( "fmt" "image" + "image/color" "image/draw" "math" "os" @@ -20,6 +21,7 @@ import ( // Deck is a set of widgets. type Deck struct { File string + Config DeckConfig Background image.Image Widgets []Widget } @@ -38,7 +40,8 @@ func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) { } d := Deck{ - File: path, + Config: dc, + File: path, } if dc.Background != "" { bgpath, err := expandPath(filepath.Dir(path), dc.Background) @@ -109,10 +112,33 @@ func (d Deck) backgroundForKey(dev *streamdeck.Device, key uint8) image.Image { pixels := int(dev.Pixels) bg := image.NewRGBA(image.Rect(0, 0, pixels, pixels)) - if d.Background != nil { + keyConfig := KeyConfig{} + for _, conf := range d.Config.Keys { + if conf.Index == key { + keyConfig = conf + break + } + } + if d.Background != nil || keyConfig.BackgroundColor != "" { startx := int(key%dev.Columns) * (pixels + padding) starty := int(key/dev.Columns) * (pixels + padding) - draw.Draw(bg, bg.Bounds(), d.Background, image.Point{startx, starty}, draw.Src) + bgDrawn := false + if d.Background != nil && keyConfig.BackgroundColor != "" && keyConfig.BackgroundMode != "blend" { + draw.Draw(bg, bg.Bounds(), d.Background, image.Point{startx, starty}, draw.Over) + bgDrawn = true + } + if keyConfig.BackgroundColor != "" { + var fillColor color.Color + err := ConfigValue(keyConfig.BackgroundColor, &fillColor) + if err != nil { + verbosef("Unabled to parse backgroundColor for key %d", key) + } else { + draw.Draw(bg, bg.Bounds(), &image.Uniform{fillColor}, image.Point{startx, starty}, draw.Src) + } + } + if !bgDrawn && d.Background != nil { + draw.Draw(bg, bg.Bounds(), d.Background, image.Point{startx, starty}, draw.Over) + } } return bg