Created
March 4, 2019 02:34
-
-
Save krry/6db332b176b4b0aadfcf55b78b566d9a to your computer and use it in GitHub Desktop.
An answer to A Tour of Go: Exercise: Images
This file contains 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" | |
"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