github.com/Kintar/etxt@v0.0.0-20221224033739-2fc69f000137/examples/gtxt/mix_modes/main.go (about) 1 //go:build gtxt 2 3 package main 4 5 import "os" 6 import "image" 7 import "image/color" 8 import "image/png" 9 import "path/filepath" 10 import "log" 11 import "fmt" 12 13 import "github.com/Kintar/etxt" 14 15 // Must be compiled with '-tags gtxt' 16 17 // TODO: it would be nice to make an extra example with semi-transparent 18 // colors, mostly to confirm that all the modes are working as 19 // expected, which I don't trust very much. 20 21 func main() { 22 // get font path 23 if len(os.Args) != 2 { 24 msg := "Usage: expects one argument with the path to the font to be used\n" 25 fmt.Fprint(os.Stderr, msg) 26 os.Exit(1) 27 } 28 29 // parse font 30 font, fontName, err := etxt.ParseFontFrom(os.Args[1]) 31 if err != nil { 32 log.Fatal(err) 33 } 34 fmt.Printf("Font loaded: %s\n", fontName) 35 36 // create cache 37 cache := etxt.NewDefaultCache(1024 * 1024 * 1024) // 1GB cache 38 39 // create and configure renderer 40 renderer := etxt.NewStdRenderer() 41 renderer.SetCacheHandler(cache.NewHandler()) 42 renderer.SetSizePx(24) 43 renderer.SetFont(font) 44 renderer.SetAlign(etxt.YCenter, etxt.XCenter) 45 renderer.SetColor(color.RGBA{0, 0, 0, 255}) // black 46 47 // create target image and fill it with different colors 48 outImage := image.NewRGBA(image.Rect(0, 0, 720, 300)) 49 for i := 0; i < 720*100*4; i += 4 { // first 100 lines cyan 50 //outImage.Pix[i + 0] = 0 51 outImage.Pix[i+1] = 255 52 outImage.Pix[i+2] = 255 53 outImage.Pix[i+3] = 255 54 } 55 for i := 720 * 100 * 4; i < 720*200*4; i += 4 { // next 100 lines magenta 56 outImage.Pix[i+0] = 255 57 //outImage.Pix[i + 1] = 0 58 outImage.Pix[i+2] = 255 59 outImage.Pix[i+3] = 255 60 } 61 for i := 720 * 200 * 4; i < 720*300*4; i += 4 { // next 100 lines yellow 62 outImage.Pix[i+0] = 255 63 outImage.Pix[i+1] = 255 64 //outImage.Pix[i + 2] = 0 65 outImage.Pix[i+3] = 255 66 } 67 68 // set target 69 renderer.SetTarget(outImage) 70 71 // draw first row of mix modes 72 offX, offY := 180, 100 73 x := offX / 2 74 y := offY / 2 75 renderer.Draw("over", x, y) 76 77 x += offX 78 renderer.SetMixMode(etxt.MixCut) 79 renderer.Draw("cut", x, y) 80 81 x += offX 82 renderer.SetMixMode(etxt.MixReplace) 83 renderer.Draw("replace", x, y) 84 85 x += offX 86 renderer.SetMixMode(etxt.MixFiftyFifty) 87 renderer.Draw("50%-50%", x, y) 88 89 // draw second row of mix modes 90 y += offY 91 x = offX / 2 92 renderer.SetColor(color.RGBA{0, 255, 255, 255}) 93 renderer.SetMixMode(etxt.MixSub) 94 renderer.Draw("subtract", x, y) 95 96 x += offX 97 renderer.SetMixMode(etxt.MixAdd) 98 renderer.Draw("add", x, y) 99 100 x += offX 101 renderer.SetMixMode(etxt.MixMultiply) 102 renderer.Draw("multiply", x, y) 103 104 x += offX 105 renderer.SetMixMode(etxt.MixOver) 106 renderer.Draw("over", x, y) 107 108 // draw third row of mix modes 109 y += offY 110 x = offX / 2 111 renderer.SetColor(color.RGBA{255, 0, 0, 255}) 112 renderer.SetMixMode(etxt.MixOver) 113 renderer.Draw("over", x, y) 114 115 x += offX 116 renderer.SetMixMode(etxt.MixFiftyFifty) 117 renderer.Draw("50%-50%", x, y) 118 119 x += offX 120 renderer.SetMixMode(etxt.MixSub) 121 renderer.Draw("subtract", x, y) 122 123 x += offX 124 renderer.SetMixMode(etxt.MixMultiply) 125 renderer.Draw("multiply", x, y) 126 127 // store image as png 128 filename, err := filepath.Abs("gtxt_mix_modes.png") 129 if err != nil { 130 log.Fatal(err) 131 } 132 fmt.Printf("Output image: %s\n", filename) 133 file, err := os.Create(filename) 134 if err != nil { 135 log.Fatal(err) 136 } 137 err = png.Encode(file, outImage) 138 if err != nil { 139 log.Fatal(err) 140 } 141 err = file.Close() 142 if err != nil { 143 log.Fatal(err) 144 } 145 fmt.Print("Program exited successfully.\n") 146 }