Last active
January 1, 2020 13:52
-
-
Save eczn/9b31e56aeed1feb66ac97331e9a7b242 to your computer and use it in GitHub Desktop.
Go 语言 Image interface 实例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
Author
eczn
commented
Jan 1, 2020


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