Skip to content

Commit

Permalink
Color param for drawString and add color attribute for all text-based…
Browse files Browse the repository at this point in the history
… widgets
  • Loading branch information
zMoooooritz authored and muesli committed Jun 8, 2021
1 parent 7d43a7b commit 41061de
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ A simple button that can display an image and/or a label.
icon = "/some/image.png"
label = "My Button"
fontsize = 10.0
color = "#fefefe"
```

The config options `icon`, `label`, and `fontsize` are optional.
Expand All @@ -158,6 +159,7 @@ A flexible widget that can display the current time or date.
[keys.widget.config]
format = "%H;%i;%s"
font = "bold;regular;thin"
color = "#fefefe"
```

Values for `format` are:
Expand Down Expand Up @@ -188,6 +190,7 @@ This widget shows the current CPU or memory utilization as a bar graph.
id = "top"
[keys.widget.config]
mode = "cpu"
color = "#fefefe"
fillColor = "#d497de"
```

Expand Down
8 changes: 6 additions & 2 deletions widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
"github.com/nfnt/resize"
)

var (
DefaultColor = color.RGBA{255, 255, 255, 255}
)

// Widget is an interface implemented by all available widgets.
type Widget interface {
Key() uint8
Expand Down Expand Up @@ -173,7 +177,7 @@ func drawImage(img *image.RGBA, icon image.Image, size int, pt image.Point) erro
return nil
}

func drawString(img *image.RGBA, bounds image.Rectangle, ttf *truetype.Font, text string, dpi uint, fontsize float64, pt image.Point) {
func drawString(img *image.RGBA, bounds image.Rectangle, ttf *truetype.Font, text string, dpi uint, fontsize float64, color color.Color, pt image.Point) {
c := ftContext(img, ttf, dpi, fontsize)

if fontsize <= 0 {
Expand All @@ -198,7 +202,7 @@ func drawString(img *image.RGBA, bounds image.Rectangle, ttf *truetype.Font, tex
pt = image.Pt(pt.X, bounds.Min.Y+int(ycenter))
}

c.SetSrc(image.NewUniform(color.RGBA{255, 255, 255, 255}))
c.SetSrc(image.NewUniform(color))
if _, err := c.DrawString(text, freetype.Pt(pt.X, pt.Y)); err != nil {
log.Fatal(err)
}
Expand Down
9 changes: 9 additions & 0 deletions widget_button.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"image"
"image/color"

"github.com/muesli/streamdeck"
)
Expand All @@ -13,6 +14,7 @@ type ButtonWidget struct {
icon image.Image
label string
fontsize float64
color color.Color
}

// NewButtonWidget returns a new ButtonWidget.
Expand All @@ -24,11 +26,14 @@ func NewButtonWidget(bw BaseWidget, opts WidgetConfig) (*ButtonWidget, error) {
_ = ConfigValue(opts.Config["label"], &label)
var fontsize float64
_ = ConfigValue(opts.Config["fontsize"], &fontsize)
var color color.Color
_ = ConfigValue(opts.Config["color"], &color)

w := &ButtonWidget{
BaseWidget: bw,
label: label,
fontsize: fontsize,
color: color,
}

if icon != "" {
Expand Down Expand Up @@ -69,13 +74,17 @@ func (w *ButtonWidget) Update(dev *streamdeck.Device) error {
bounds.Min.Y += iconsize + margin
bounds.Max.Y -= margin
}
if w.color == nil {
w.color = DefaultColor
}

drawString(img,
bounds,
ttfFont,
w.label,
dev.DPI,
w.fontsize,
w.color,
image.Pt(-1, -1))
} else if w.icon != nil {
err := drawImage(img,
Expand Down
10 changes: 10 additions & 0 deletions widget_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"image"
"image/color"
"strings"
"time"

Expand All @@ -15,6 +16,7 @@ type TimeWidget struct {

format string
font string
color color.Color
}

// NewTimeWidget returns a new TimeWidget.
Expand All @@ -24,11 +26,14 @@ func NewTimeWidget(bw BaseWidget, opts WidgetConfig) *TimeWidget {
var format, font string
_ = ConfigValue(opts.Config["format"], &format)
_ = ConfigValue(opts.Config["font"], &font)
var color color.Color
_ = ConfigValue(opts.Config["color"], &color)

return &TimeWidget{
BaseWidget: bw,
format: format,
font: font,
color: color,
}
}

Expand All @@ -49,6 +54,10 @@ func (w *TimeWidget) Update(dev *streamdeck.Device) error {
fonts = append(fonts, "regular")
}

if w.color == nil {
w.color = DefaultColor
}

for i := 0; i < len(formats); i++ {
str := formatTime(time.Now(), formats[i])
font := fontByName(fonts[i])
Expand All @@ -60,6 +69,7 @@ func (w *TimeWidget) Update(dev *streamdeck.Device) error {
str,
dev.DPI,
-1,
w.color,
image.Pt(-1, -1))
}

Expand Down
24 changes: 14 additions & 10 deletions widget_top.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"log"
"strconv"

colorful "github.com/lucasb-eyer/go-colorful"
"github.com/muesli/streamdeck"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/mem"
Expand All @@ -19,7 +18,8 @@ type TopWidget struct {
BaseWidget

mode string
fillColor string
color color.Color
fillColor color.Color

lastValue float64
}
Expand All @@ -28,13 +28,16 @@ type TopWidget struct {
func NewTopWidget(bw BaseWidget, opts WidgetConfig) *TopWidget {
bw.setInterval(opts.Interval, 500)

var mode, fillColor string
var mode string
_ = ConfigValue(opts.Config["mode"], &mode)
var color, fillColor color.Color
_ = ConfigValue(opts.Config["color"], &color)
_ = ConfigValue(opts.Config["fillColor"], &fillColor)

return &TopWidget{
BaseWidget: bw,
mode: mode,
color: color,
fillColor: fillColor,
}
}
Expand Down Expand Up @@ -71,12 +74,11 @@ func (w *TopWidget) Update(dev *streamdeck.Device) error {
}
w.lastValue = value

if w.fillColor == "" {
w.fillColor = "#a69bd6"
if w.color == nil {
w.color = DefaultColor
}
fill, err := colorful.Hex(w.fillColor)
if err != nil {
return fmt.Errorf("invalid color: %s", w.fillColor)
if w.fillColor == nil {
w.fillColor = color.RGBA{166, 155, 182, 255}
}

size := int(dev.Pixels)
Expand All @@ -85,15 +87,15 @@ func (w *TopWidget) Update(dev *streamdeck.Device) error {

draw.Draw(img,
image.Rect(12, 6, size-12, size-18),
&image.Uniform{color.RGBA{255, 255, 255, 255}},
&image.Uniform{w.color},
image.Point{}, draw.Src)
draw.Draw(img,
image.Rect(13, 7, size-14, size-20),
&image.Uniform{color.RGBA{0, 0, 0, 255}},
image.Point{}, draw.Src)
draw.Draw(img,
image.Rect(14, 7+int(float64(size-26)*(1-value/100)), size-15, size-21),
&image.Uniform{fill},
&image.Uniform{w.fillColor},
image.Point{}, draw.Src)

// draw percentage
Expand All @@ -107,6 +109,7 @@ func (w *TopWidget) Update(dev *streamdeck.Device) error {
strconv.FormatInt(int64(value), 10),
dev.DPI,
13,
w.color,
image.Pt(-1, -1))

// draw description
Expand All @@ -120,6 +123,7 @@ func (w *TopWidget) Update(dev *streamdeck.Device) error {
"% "+label,
dev.DPI,
-1,
w.color,
image.Pt(-1, -1))

return w.render(dev, img)
Expand Down

0 comments on commit 41061de

Please sign in to comment.