Added scaled ImageSource.

This commit is contained in:
Sander Schobers 2020-12-12 14:41:14 +01:00
parent 0f03760e66
commit cc32cf5bc3

View File

@ -3,6 +3,7 @@ package ui
import (
"image"
"github.com/nfnt/resize"
"opslag.de/schobers/zntg"
)
@ -45,3 +46,25 @@ func (s ImageSourceResource) CreateImage() (image.Image, error) {
}
return value.(image.Image), nil
}
func ImageSourceFromResources(res Resources, name string) ImageSourceResource {
return ImageSourceResource{Resources: res, Name: name}
}
func Scaled(source ImageSource, width, height int) ImageSource {
return scaledImageSource{ImageSource: source, width: width, height: height}
}
type scaledImageSource struct {
ImageSource
width, height int
}
func (s scaledImageSource) CreateImage() (image.Image, error) {
im, err := s.ImageSource.CreateImage()
if err != nil {
return nil, err
}
return resize.Resize(uint(s.width), uint(s.height), im, resize.Bicubic), nil
}