Skip to content

Commit 373f261

Browse files
committed
Auto-detect image size, for later resizing
1 parent 16f2bc3 commit 373f261

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

buttons/imagefile.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package buttons
22

33
import (
4+
"github.com/disintegration/gift"
45
"image"
56
"image/draw"
67
"os"
@@ -20,9 +21,10 @@ type ImageFileButton struct {
2021

2122
// GetImageForButton is the interface implemention to get the button's image as an image.Image
2223
func (btn *ImageFileButton) GetImageForButton(btnSize int) image.Image {
23-
// TODO base the 96 on the image bounds
24-
newimg := image.NewRGBA(image.Rect(0, 0, btnSize, btnSize))
25-
draw.Draw(newimg, newimg.Bounds(), btn.img, image.Point{0, 0}, draw.Src)
24+
// Resize the image to what the button wants
25+
g := gift.New(gift.Resize(btnSize, btnSize, gift.LanczosResampling))
26+
newimg := image.NewRGBA(image.Rect(0, 0, btn.img.Bounds().Max.X, btn.img.Bounds().Max.Y))
27+
g.Draw(newimg, btn.img)
2628
return newimg
2729
}
2830

@@ -37,17 +39,17 @@ func (btn *ImageFileButton) GetButtonIndex() int {
3739
}
3840

3941
// SetFilePath allows the image file to be changed on the fly
40-
func (btn *ImageFileButton) SetFilePath(filePath string, btnSize int) error {
42+
func (btn *ImageFileButton) SetFilePath(filePath string) error {
4143
btn.filePath = filePath
42-
err := btn.loadImage(btnSize)
44+
err := btn.loadImage()
4345
if err != nil {
4446
return err
4547
}
4648
btn.updateHandler(btn)
4749
return nil
4850
}
4951

50-
func (btn *ImageFileButton) loadImage(btnSize int) error {
52+
func (btn *ImageFileButton) loadImage() error {
5153
f, err := os.Open(btn.filePath)
5254
if err != nil {
5355
return err
@@ -58,8 +60,7 @@ func (btn *ImageFileButton) loadImage(btnSize int) error {
5860
var newimg *image.RGBA
5961
newimg, ok := img.(*image.RGBA)
6062
if !ok {
61-
// TODO base the 96 on the button size
62-
newimg = image.NewRGBA(image.Rect(0, 0, btnSize, btnSize))
63+
newimg = image.NewRGBA(image.Rect(0, 0, img.Bounds().Max.X, img.Bounds().Max.Y))
6364
draw.Draw(newimg, newimg.Bounds(), img, image.Point{0, 0}, draw.Src)
6465
}
6566

@@ -91,9 +92,9 @@ func (btn *ImageFileButton) Pressed() {
9192
}
9293

9394
// NewImageFileButton creates a new ImageFileButton with the specified image on it
94-
func NewImageFileButton(filePath string, btnSize int) (*ImageFileButton, error) {
95+
func NewImageFileButton(filePath string) (*ImageFileButton, error) {
9596
btn := &ImageFileButton{filePath: filePath}
96-
err := btn.loadImage(btnSize)
97+
err := btn.loadImage()
9798
if err != nil {
9899
return nil, err
99100
}

0 commit comments

Comments
 (0)