Skip to content

Instantly share code, notes, and snippets.

@satyrius
Created October 24, 2013 20:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satyrius/7144143 to your computer and use it in GitHub Desktop.
Save satyrius/7144143 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, …
package main
import (
"code.google.com/p/go-tour/pic"
"image"
"image/color"
)
type Image struct{
Width, Height int
}
func (img Image) ColorModel() color.Model {
return color.RGBAModel
}
func (img Image) Bounds() image.Rectangle {
return image.Rect(0, 0, img.Width, img.Height)
}
func (img Image) At(x, y int) color.Color {
return color.RGBA{uint8(x * y), 0, 255, 255}
}
func main() {
m := Image{100, 100}
pic.ShowImage(m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment