Skip to content

Commit 449516e

Browse files
committed
Laying groundwork for different image formats on different devices
1 parent 8c1775f commit 449516e

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

comms.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,13 @@ func (d *Device) ClearButtons() {
132132
}
133133

134134
// WriteColorToButton writes a specified color to the given button
135-
func (d *Device) WriteColorToButton(btnIndex int, colour color.Color) {
135+
func (d *Device) WriteColorToButton(btnIndex int, colour color.Color) error {
136136
img := getSolidColourImage(colour)
137-
d.rawWriteToButton(btnIndex, getImageAsJpeg(img))
137+
imgForButton, err := getImageForButton(img, d.deviceType.imageFormat)
138+
if err != nil {
139+
return err
140+
}
141+
return d.rawWriteToButton(btnIndex, imgForButton)
138142
}
139143

140144
// WriteImageToButton writes a specified image file to the given button
@@ -190,7 +194,11 @@ func (d *Device) ResetComms() {
190194
// WriteRawImageToButton takes an `image.Image` and writes it to the given button, after resizing and rotating the image to fit the button (for some reason the StreamDeck screens are all upside down)
191195
func (d *Device) WriteRawImageToButton(btnIndex int, rawImg image.Image) error {
192196
img := resizeAndRotate(rawImg, d.deviceType.imageSize.X, d.deviceType.imageSize.Y)
193-
return d.rawWriteToButton(btnIndex, getImageAsJpeg(img))
197+
imgForButton, err := getImageForButton(img, d.deviceType.imageFormat)
198+
if err != nil {
199+
return err
200+
}
201+
return d.rawWriteToButton(btnIndex, imgForButton)
194202
}
195203

196204
func (d *Device) rawWriteToButton(btnIndex int, rawImage []byte) error {

image.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package streamdeck
22

33
import (
44
"bytes"
5+
"errors"
56
"image"
67
"image/color"
78
"image/draw"
@@ -24,10 +25,15 @@ func resizeAndRotate(img image.Image, width, height int) image.Image {
2425
return res
2526
}
2627

27-
func getImageAsJpeg(img image.Image) []byte {
28+
func getImageForButton(img image.Image, btnFormat string) ([]byte, error) {
2829
var b bytes.Buffer
29-
jpeg.Encode(&b, img, nil)
30-
return b.Bytes()
30+
switch btnFormat {
31+
case "JPEG":
32+
jpeg.Encode(&b, img, nil)
33+
default:
34+
return nil, errors.New("Unknown button image format: " + btnFormat)
35+
}
36+
return b.Bytes(), nil
3137
}
3238

3339
func getSolidColourImage(colour color.Color) *image.RGBA {

0 commit comments

Comments
 (0)