Skip to content

Instantly share code, notes, and snippets.

@dz1984
Created March 23, 2014 09:14
Show Gist options
  • Save dz1984/9720643 to your computer and use it in GitHub Desktop.
Save dz1984/9720643 to your computer and use it in GitHub Desktop.
"A Tour of Go"
/*
Exercise: Images
Remember the picture generator you wrote earlier? Let's write another one, but this time it will return an implementation of image.Image instead of a slice of data.
Define your own Image type, implement the necessary methods, and call pic.ShowImage.
Bounds should return a image.Rectangle, like image.Rect(0, 0, w, h).
ColorModel should return color.RGBAModel.
At should return a color; the value v in the last picture generator corresponds to color.RGBA{v, v, 255, 255} in this one.
*/
package main
import (
"code.google.com/p/go-tour/pic"
"image"
"image/color"
)
type Image struct{
W,H int
V uint8
}
func (im Image) Bounds() image.Rectangle{
return image.Rect(0,0,im.W,im.H)
}
func (im Image) ColorModel() color.Model {
return color.RGBAModel;
}
func (im Image) At(x,y int) color.Color {
return color.RGBA{im.V,im.V,255,255}
}
func main() {
m := Image{256,256,128}
pic.ShowImage(m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment