Created
July 8, 2013 18:03
-
-
Save slawosz/5951003 to your computer and use it in GitHub Desktop.
go images http://tour.golang.org/#59
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 ( | |
"code.google.com/p/go-tour/pic" | |
"image" | |
"image/color" | |
) | |
type Image struct{ | |
w, h int | |
pixels [][]color.RGBA | |
} | |
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.w, img.h) | |
} | |
// 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 { | |
return img.pixels[x][y] | |
} | |
func Img(w, h int) Image { | |
pixels := make([][]color.RGBA, w) | |
for i := 0; i < w; i++ { | |
pixels[i] = make([]color.RGBA, h) | |
} | |
for i := 0; i < w; i++ { | |
for j := 0; j < h; j++ { | |
pixels[i][j] = color.RGBA{uint8(w), uint8(w+j), uint8(h-j), 25} | |
} | |
} | |
return Image{w, h, pixels} | |
} | |
func main() { | |
m := Img(122,33) | |
pic.ShowImage(m) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment