Golang image.Alpha.Set and SetAlpha functions examples

package image

 func (p *Alpha) Set(x, y int, c color.Color)
  

Golang image.Alpha.Set and SetAlpha functions usage examples

Example 1 :

 package main

 import (
 "fmt"
 "image"
 "image/png"
 "image/color" // for color.Alpha{a}
 "os"
 )

 func init() {
 // without this register .. At(), Bounds() functions will
 // caused memory pointer error!!
 image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
 }

 func main() {
 imgfile, err := os.Open("./img.png")

 if err != nil {
 fmt.Println("img.png file not found!")
 os.Exit(1)
 }

 defer imgfile.Close()

 img, _, err := image.Decode(imgfile)

 bounds := img.Bounds()

 canvas := image.NewAlpha(bounds)

 canvas.Set(100, 100, image.Transparent)


 // http://golang.org/pkg/image/color/#Alpha
 // Alpha represents an 8-bit alpha color.

 x := 10
 y := 10
 a := uint8((23*x + 29*y) % 0x100)

 canvas.SetAlpha(x, y, color.Alpha{a})

 }

Example 2:

 func drawPixels(img *image.Alpha, px, py, pw, ph uint, fill bool) {
  var x, y uint
  for y = 0; y < ph; y++ {
 for x = 0; x < pw; x++ {
 if fill {
 img.Set(int(px*pw+x), int(py*ph+y), image.White)
 } else {
 img.Set(int(px*pw+x), int(py*ph+y), image.Transparent)
 }
 }
  }
 }

References :

https://github.com/jteeuwen/dcpu/blob/master/dcpu-emu/lem1802.go

http://golang.org/pkg/image/#Alpha.Set

Advertisement