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
+16k Golang : Read a file line by line
+43.7k Golang : Get hardware information such as disk, memory and CPU usage
+5.5k Golang : Get S3 or CloudFront object or file information
+6.1k Fontello : How to load and use fonts?
+6.4k PHP : Proper way to get UTF-8 character or string length
+26.4k Golang : Calculate future date with time.Add() function
+29.3k Golang : missing Git command
+14.2k Golang : syscall.Socket example
+5.4k Golang : Generate Interleaved 2 inch by 5 inch barcode
+8k Golang : Trim everything onward after a word
+6.6k Golang : Combine slices of complex numbers and operation example
+8.3k Golang : Reverse text lines or flip line order example