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
Advertisement
Something interesting
Tutorials
+15.7k Golang : How to tell if a file is compressed either gzip or zip ?
+17.9k Golang : Clearing slice
+12.1k Swift : Convert (cast) Int to String ?
+12.3k Golang : Set image canvas or background to transparent
+8.2k Golang : How to control fmt or log print format?
+12k Golang : How to get a user home directory path?
+14.1k Golang : Validate hostname
+19.6k Golang : Create and resolve(read) symbolic links
+6.3k Golang : alternative to os.Exit() function
+19.8k Golang : Get password from console input without echo or masked
+8.7k Golang : Detect number of faces or vehicles in a photo