Skip to content

Instantly share code, notes, and snippets.

@krry
Created March 4, 2019 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krry/6db332b176b4b0aadfcf55b78b566d9a to your computer and use it in GitHub Desktop.
Save krry/6db332b176b4b0aadfcf55b78b566d9a to your computer and use it in GitHub Desktop.
An answer to A Tour of Go: Exercise: Images
package main
import (
"golang.org/x/tour/pic"
"image"
"image/color"
"math/rand"
"fmt"
)
type Image struct {
w, h int
r, g, b uint8
}
func (i Image) Bounds() image.Rectangle {
return image.Rect(0, 0, i.w, i.h)
}
func (i Image) ColorModel() color.Model {
return color.RGBAModel
}
func (i Image) At (x, y int) color.Color {
return color.RGBA{i.r + uint8(x^-y), i.g + uint8(y-^x), i.b + uint8(x*y), 255}
}
// Uint8 will generate a random uint8 value
func Uint8() uint8 {
// not really random at all
return uint8(rand.Int31())
}
func main() {
fmt.Println(Uint8())
m := Image{256, 256, Uint8(), Uint8(), Uint8()}
pic.ShowImage(m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment