1 // Package limage introduces structures to read and build layered images 2 package limage 3 4 import ( 5 "image" 6 "image/color" 7 8 "vimagination.zapto.org/limage/internal" 9 ) 10 11 // MaskedImage represents an image that has a to-be-applied mask 12 type MaskedImage struct { 13 image.Image 14 Mask *image.Gray 15 } 16 17 // At returns the colour at the specified coords after masking 18 func (m MaskedImage) At(x, y int) color.Color { 19 return transparency(m.Image.At(x, y), m.Mask.GrayAt(x, y).Y) 20 } 21 22 // Text represents a text layer 23 type Text struct { 24 image.Image 25 TextData 26 } 27 28 // TextData represents the stylised text 29 type TextData []TextDatum 30 31 // String returns a flattened string 32 func (t TextData) String() string { 33 toRet := "" 34 for _, d := range t { 35 toRet += d.Data 36 } 37 return toRet 38 } 39 40 // TextDatum is a collection of styling for a single piece of text 41 type TextDatum struct { 42 ForeColor, BackColor color.Color 43 Size, LetterSpacing, Rise uint32 44 Bold, Italic, Underline, Strikethrough bool 45 Font, Data string 46 } 47 48 func transparency(ac color.Color, ao uint8) color.Color { 49 if ao == 0xff { 50 return ac 51 } else if ao == 0 { 52 return color.NRGBA64{} 53 } 54 c := internal.ColourToNRGBA(ac) 55 o := uint32(ao) 56 o |= o << 8 57 c.A = uint16(o * uint32(c.A) / 0xffff) 58 return c 59 } 60