1 package limage // import "vimagination.zapto.org/limage" 2 3 import ( 4 "image" 5 "image/color" 6 7 "vimagination.zapto.org/limage/internal" 8 ) 9 10 // Image represents a collection of layers 11 type Image []Layer 12 13 // ColorModel represents the color model of the group. It uses the first layer 14 // to determine the color model 15 func (g Image) ColorModel() color.Model { 16 if len(g) == 0 { 17 return color.AlphaModel 18 } 19 return g[0].ColorModel() 20 } 21 22 // Bounds returns the limits for the dimensions of the group 23 func (g Image) Bounds() image.Rectangle { 24 var r image.Rectangle 25 for _, l := range g { 26 r = r.Union(l.Bounds()) 27 } 28 return r 29 } 30 31 // At returns the colour at the specified coords 32 func (g Image) At(x, y int) color.Color { 33 c := color.Color(color.Alpha{}) 34 point := image.Point{x, y} 35 for i := len(g) - 1; i >= 0; i-- { 36 if g[i].Invisible { 37 continue 38 } 39 if !point.In(g[i].Bounds()) { 40 continue 41 } 42 if _, ok := g.ColorModel().(color.Palette); g[i].Mode != CompositeDissolve && ok { 43 if d := internal.ColourToNRGBA(g[i].At(x, y)); d.A > 0x7fff { 44 d.A = 0xffff 45 c = d 46 } 47 } else { 48 c = g[i].Mode.Composite(c, g[i].At(x, y)) 49 } 50 } 51 return c 52 } 53 54 // SubImage returns an image representing the portion of the image p visible 55 // through r. The returned value shares pixels with the original image 56 func (g Image) SubImage(r image.Rectangle) image.Image { 57 h := make(Image, len(g)) 58 for n, l := range g { 59 h[n] = l.SubImageLayer(r) 60 } 61 return h 62 } 63