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
+18.6k Golang : Iterating Elements Over A List
+3.7k Golang : Switch Redis database redis.NewClient
+8.9k Golang : Sort lines of text example
+12.3k Golang : Get month name from date example
+16.4k Golang : Convert slice to array
+15.6k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+28.6k Golang : Read, Write(Create) and Delete Cookie example
+22.9k Golang : Test file read write permission example
+16.3k Golang :Trim white spaces from a string
+12.8k Golang : Listen and Serve on sub domain example
+7k Golang : How to call function inside template with template.FuncMap
+12.6k Golang : flag provided but not defined error