Created
November 17, 2018 03:29
-
-
Save fr-xiaoli/3769b5d8b5911b30b62d0c7fa08b404e to your computer and use it in GitHub Desktop.
A Tour of Go - Methods and interfaces - Exercise: Images
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 Image struct{ | |
| w int | |
| h int | |
| } | |
| func (Image) ColorModel() color.Model { | |
| return color.RGBAModel | |
| } | |
| func (img Image) Bounds() image.Rectangle { | |
| return image.Rect(0, 0, img.w, img.h) | |
| } | |
| func (Image) At(x, y int) color.Color { | |
| //v := uint8((x + y) / 2) // gradient | |
| v := uint8(x*y) // hypnotize | |
| //v := uint8(x^y) // gradient checkers | |
| return color.RGBA{v, v, 255, 255} | |
| } | |
| func main() { | |
| m := Image{ 600, 250 } | |
| pic.ShowImage(m) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment