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