Skip to content

Instantly share code, notes, and snippets.

@fr-xiaoli
Created November 17, 2018 03:29
Show Gist options
  • Select an option

  • Save fr-xiaoli/3769b5d8b5911b30b62d0c7fa08b404e to your computer and use it in GitHub Desktop.

Select an option

Save fr-xiaoli/3769b5d8b5911b30b62d0c7fa08b404e to your computer and use it in GitHub Desktop.
A Tour of Go - Methods and interfaces - Exercise: Images
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