Skip to content

Instantly share code, notes, and snippets.

@eczn
Last active January 1, 2020 13:52
Show Gist options
  • Select an option

  • Save eczn/9b31e56aeed1feb66ac97331e9a7b242 to your computer and use it in GitHub Desktop.

Select an option

Save eczn/9b31e56aeed1feb66ac97331e9a7b242 to your computer and use it in GitHub Desktop.
Go 语言 Image interface 实例
package main
import (
"golang.org/x/tour/pic"
"image"
"image/color"
)
type MyImage struct{
size int
render func (x int, y int) uint8
}
// 定义图片的范围
func (img MyImage) Bounds() image.Rectangle {
return image.Rect(0, 0, img .size, img.size)
}
// 定义图片的颜色模式, 这里直接使用了 rgba 通道模式
func (img MyImage) ColorModel() color.Model {
return color.RGBAModel
}
// 定义获取某个点的 Color
func (img MyImage) At(x int, y int) color.Color {
var v = img.render(x, y)
var c = color.RGBA{
R: v, G: v, B: v, A: 255,
}
return c
}
func main() {
// 构造 MyImage 的实例
var myImg = MyImage{
size: 256,
render: func(x int, y int) uint8 {
return uint8((x + y) / 2)
},
}
// 将 Image 转 base64
pic.ShowImage(myImg)
}
@eczn
Copy link
Copy Markdown
Author

eczn commented Jan 1, 2020

image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment