img.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package gg
  2. import (
  3. "github.com/hajimehoshi/ebiten/v2"
  4. "image"
  5. _ "image/png"
  6. "io"
  7. "math"
  8. )
  9. import "surdeus.su/core/gg/mx"
  10. type Image = ebiten.Image
  11. type ImageRect = image.Rectangle
  12. type ImagePoint = image.Point
  13. type ColorValue uint32
  14. type ColorMode = ebiten.ColorM
  15. type Color struct {
  16. R, G, B, A ColorValue
  17. }
  18. const (
  19. MaxColorValue = math.MaxUint32
  20. )
  21. // The wrapper to make RGBA color via
  22. // values from 0 to 1 (no value at all and the max value).
  23. func RGBA(r, g, b, a mx.Float) Color {
  24. return Color {
  25. ColorValue(r*MaxColorValue),
  26. ColorValue(g*MaxColorValue),
  27. ColorValue(b*MaxColorValue),
  28. ColorValue(a*MaxColorValue),
  29. }
  30. }
  31. // Read an image from Reader and return
  32. // a new *Image.
  33. func LoadImage(input io.Reader) (*Image, error) {
  34. img, _, err := image.Decode(input)
  35. if err != nil {
  36. return nil, err
  37. }
  38. ret := ebiten.NewImageFromImage(img)
  39. return ret, nil
  40. }
  41. // Returns a new empty image
  42. // with specified with and height.
  43. func NewImage(w, h int) (*Image) {
  44. return ebiten.NewImage(w, h)
  45. }
  46. func NewImageFromImage(img image.Image) *Image {
  47. return ebiten.NewImageFromImage(img)
  48. }
  49. func (c Color) RGBA() (r, g, b, a uint32) {
  50. return uint32(c.R), uint32(c.G), uint32(c.B), uint32(c.A)
  51. }