mirror of
https://github.com/Luzifer/streamdeck.git
synced 2024-12-20 09:41:19 +00:00
Make moving between SDs easier by adjusting images
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
47e2c001b8
commit
42c0ec2f11
5 changed files with 45 additions and 0 deletions
|
@ -81,6 +81,8 @@ func (d displayElementExec) Display(idx int, attributes map[string]interface{})
|
|||
return errors.Wrap(err, "Unable to get image from disk")
|
||||
}
|
||||
|
||||
bgi = autoSizeImage(bgi, sd.IconSize())
|
||||
|
||||
draw.Draw(img, img.Bounds(), bgi, image.ZP, draw.Src)
|
||||
}
|
||||
|
||||
|
|
|
@ -32,5 +32,7 @@ func (d displayElementImage) Display(idx int, attributes map[string]interface{})
|
|||
return errors.Wrap(err, "Umable to decode image")
|
||||
}
|
||||
|
||||
img = autoSizeImage(img, sd.IconSize())
|
||||
|
||||
return errors.Wrap(sd.FillImage(idx, img), "Unable to set image")
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ require (
|
|||
github.com/fsnotify/fsnotify v1.4.7
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
||||
github.com/pkg/errors v0.8.1
|
||||
github.com/sashko/go-uinput v0.0.0-20180923134002-15fcac7aa54a
|
||||
github.com/sirupsen/logrus v1.4.2
|
||||
|
|
|
@ -16,6 +16,8 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv
|
|||
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/leekchan/gtf v0.0.0-20190214083521-5fba33c5b00b/go.mod h1:thNruaSwydMhkQ8dXzapABF9Sc1Tz08ZBcDdgott9RA=
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
|
|
38
cmd/streamdeck/helpers.go
Normal file
38
cmd/streamdeck/helpers.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"github.com/nfnt/resize"
|
||||
"golang.org/x/image/draw"
|
||||
)
|
||||
|
||||
func autoSizeImage(img image.Image, size int) image.Image {
|
||||
if img.Bounds().Max.X == size && img.Bounds().Max.Y == size {
|
||||
// Image has perfect size: Nothing to change
|
||||
return img
|
||||
}
|
||||
|
||||
// Scale down when required
|
||||
img = resize.Thumbnail(uint(size), uint(size), img, resize.Lanczos2)
|
||||
if img.Bounds().Max.X == size && img.Bounds().Max.Y == size {
|
||||
// Image has perfect size now: Nothing to change
|
||||
return img
|
||||
}
|
||||
|
||||
// Image is too small, need to pad it
|
||||
var (
|
||||
dimg = image.NewNRGBA(image.Rect(0, 0, size, size))
|
||||
pt = image.Point{
|
||||
X: (size - img.Bounds().Max.X) / 2,
|
||||
Y: (size - img.Bounds().Max.Y) / 2,
|
||||
}
|
||||
)
|
||||
// Draw black background
|
||||
draw.Copy(dimg, image.Point{}, image.NewUniform(color.RGBA{0x0, 0x0, 0x0, 0xff}), dimg.Bounds(), draw.Src, nil)
|
||||
// Copy in image
|
||||
draw.Copy(dimg, pt, img, img.Bounds(), draw.Src, nil)
|
||||
|
||||
return dimg
|
||||
}
|
Loading…
Reference in a new issue