Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting a solid key background and blending with the background. #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 29 additions & 3 deletions deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"math"
"os"
Expand All @@ -20,6 +21,7 @@ import (
// Deck is a set of widgets.
type Deck struct {
File string
Config DeckConfig
Background image.Image
Widgets []Widget
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down