gg/img.go
2024-05-28 13:24:12 +05:00

63 lines
1.2 KiB
Go

package gg
import (
"github.com/hajimehoshi/ebiten/v2"
"image"
_ "image/png"
"io"
"math"
)
import "surdeus.su/core/gg/mx"
type Image = ebiten.Image
type ImageRect = image.Rectangle
type ImagePoint = image.Point
type ColorValue uint32
type ColorMode = ebiten.ColorM
type Color struct {
R, G, B, A ColorValue
}
const (
MaxColorValue = math.MaxUint32
)
// The wrapper to make RGBA color via
// values from 0 to 1 (no value at all and the max value).
func RGBA(r, g, b, a mx.Float) Color {
return Color {
ColorValue(r*MaxColorValue),
ColorValue(g*MaxColorValue),
ColorValue(b*MaxColorValue),
ColorValue(a*MaxColorValue),
}
}
// Read an image from Reader and return
// a new *Image.
func LoadImage(input io.Reader) (*Image, error) {
img, _, err := image.Decode(input)
if err != nil {
return nil, err
}
ret := ebiten.NewImageFromImage(img)
return ret, nil
}
// Returns a new empty image
// with specified with and height.
func NewImage(w, h int) (*Image) {
return ebiten.NewImage(w, h)
}
func NewImageFromImage(img image.Image) *Image {
return ebiten.NewImageFromImage(img)
}
func (c Color) RGBA() (r, g, b, a uint32) {
return uint32(c.R), uint32(c.G), uint32(c.B), uint32(c.A)
}