A Tour of Go 59 Exercise: Images

演習:Image 型を定義して、 必要なメソッド を実装し、 pic.ShowImage を呼び出す。

 よし出来た。オラわくわくして来たぞo(^o^o)(o^o^)o

最後の画像ジェネレーターの値 v は、 color.RGBA{v, v, 255, 255} のものと一致します。

上記は意味不明だったので無視w;

package main

import (
    "code.google.com/p/go-tour/pic"
    i "image"
    "image/color"
)

type Image struct{
    Pix []uint8
    Rect i.Rectangle
}

func (p *Image) ColorModel() color.Model { return color.RGBAModel }

func (p *Image) Bounds() i.Rectangle { return p.Rect }

func (p *Image) At(x, y int) color.Color {
    if !(i.Point{x, y}.In(p.Rect)) {
		return color.RGBA{}
	}
    return color.RGBA{uint8(x),uint8(y),255,255}//{0,0,255,255}
}

func NewImage(r i.Rectangle) i.Image {
    w, h := r.Dx(), r.Dy()
    buf := make([]uint8, 4*w*h)
    return &Image{buf, r}
}

func main() {
    m := NewImage(i.Rect(0, 0, 100, 100))
    pic.ShowImage(m)
}

import i "image" 出来るからやってみたら、あら読みづらい><