Skip to content

Instantly share code, notes, and snippets.

@zh-f
Created June 11, 2020 01:56
Show Gist options
  • Select an option

  • Save zh-f/80d143ef0a5924dca4eae09f49ebcf0a to your computer and use it in GitHub Desktop.

Select an option

Save zh-f/80d143ef0a5924dca4eae09f49ebcf0a to your computer and use it in GitHub Desktop.
Solution of Exercise: Images
package main
import (
"golang.org/x/tour/pic"
"image"
"image/color"
)
type Image struct{
width, height int
}
// ColorModel returns the Image's color model.
func (img *Image) ColorModel() color.Model {
return color.RGBAModel
}
// Bounds returns the domain for which At can return non-zero color.
// The bounds do not necessarily contain the point (0, 0).
func (img *Image) Bounds() image.Rectangle {
return image.Rect(0, 0, img.width, img.height)
}
// At returns the color of the pixel at (x, y).
// At(Bounds().Min.X, Bounds().Min.Y) returns the upper-left pixel of the grid.
// At(Bounds().Max.X-1, Bounds().Max.Y-1) returns the lower-right one.
func (img *Image) At(x, y int) color.Color {
r, g, b := uint8(x^y), uint8(x*y), uint8((x+y)/2)
return color.RGBA{r, g, b, 255}
}
func main() {
m := Image{1000, 1000}
pic.ShowImage(&m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment