go语言Exercise: Images
2012-05-09 16:08
344 查看
Remember the picture generator you wrote earlier? Let's write another one, but this time it will return an implementation of
of a slice of data.
Define your own
and call
like
the last picture generator corresponds to
package main
import (
"image"
"image/color"
"tour/pic"
)
type Image struct{}
func (i *Image) ColorModel() color.Model {
return color.RGBAModel
}
func (i *Image) Bounds() image.Rectangle {
return image.Rect(0, 0, 256, 256)
}
func (i *Image) At(x, y int) color.Color {
v := (uint8)(x^y)
return color.RGBA{v, v, 255, 255}
}
func main() {
m := &Image{}
pic.ShowImage(m)
}
image.Imageinstead
of a slice of data.
Define your own
Imagetype, implement the necessary methods,
and call
pic.ShowImage.
Boundsshould return a
image.Rectangle,
like
image.Rect(0, 0, w, h).
ColorModelshould return
color.RGBAModel.
Atshould return a color; the value
vin
the last picture generator corresponds to
color.RGBA{v, v, 255, 255}in this one.
package main
import (
"image"
"image/color"
"tour/pic"
)
type Image struct{}
func (i *Image) ColorModel() color.Model {
return color.RGBAModel
}
func (i *Image) Bounds() image.Rectangle {
return image.Rect(0, 0, 256, 256)
}
func (i *Image) At(x, y int) color.Color {
v := (uint8)(x^y)
return color.RGBA{v, v, 255, 255}
}
func main() {
m := &Image{}
pic.ShowImage(m)
}
相关文章推荐
- go语言Advanced Exercise: Complex cube roots
- go语言Exercise: Errors
- go语言Exercise: Rot13 Reader
- Go 语言实现exercise--Loops and Functions
- go语言Exercise: Equivalent Binary Trees
- A Tour of Go Exercise: Images
- go语言Exercise: Maps
- go语言Exercise: Slices
- 【GoLang笔记】A Tour of Go - Exercise: Images
- go语言Exercise: Fibonacci closure
- go语言中range用法
- Go语言的类型转化
- Go语言学习之map (The way to go)
- GO语言入门(1)——基本语法
- 产品环境中 Go 语言的最佳实践
- go 语言开发工具配置
- go语言里边的关键字
- 为什么要使用 Go 语言,Go 语言的优势在哪里?
- 【Go语言】【18】GO语言的select
- 七牛云 GO 语言周报【七月第 2 期】