Created
October 24, 2013 20:11
-
-
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, …
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 ( | |
"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