~mendelmaleh/dummy

72c6f59ce17e8af440766b9b9a3d8aa002e38d7c — Mendel E 1 year, 7 months ago 5c22aa9
Move image generation to image.go, extract drawDiagonal
2 files changed, 36 insertions(+), 20 deletions(-)

A image.go
M main.go
A image.go => image.go +36 -0
@@ 0,0 1,36 @@
package main

import (
	"image"
	"image/color"
)

type img struct {
	*image.RGBA
}

func Image(opt Options) image.Image {
	m := img{image.NewRGBA(image.Rectangle{image.Point{}, image.Point{opt.Width, opt.Height}})}

	switch opt.Pattern {
	case "diagonal":
		var c [2]color.RGBA
		copy(c[:], opt.Colors)
		m.drawDiagonal(opt.Width, opt.Height, c)
	}

	return m
}

func (m *img) drawDiagonal(w, h int, c [2]color.RGBA) {
	for x := 0; x < w; x++ {
		for y := 0; y < h; y++ {
			// if y < h/w)*x) {
			if y <= h-x {
				m.Set(x, y, c[0])
			} else {
				m.Set(x, y, c[1])
			}
		}
	}
}

M main.go => main.go +0 -20
@@ 3,7 3,6 @@ package main
import (
	"fmt"
	"html/template"
	"image"
	"image/png"
	"io/ioutil"
	"log"


@@ 116,22 115,3 @@ func Web(w http.ResponseWriter, r *http.Request, opt Options, config Config, tmp
	}{"Dummy", opt.Desc, template.URL(u.String())})

}

func Image(opt Options) image.Image {
	img := image.NewRGBA(image.Rectangle{image.Point{}, image.Point{opt.Width, opt.Height}})

	for x := 0; x < opt.Width; x++ {
		for y := 0; y < opt.Height; y++ {
			// if y < opt.Height-((opt.Height/opt.Width)*x) {
			if y <= opt.Height-x {
				img.Set(x, y, opt.Colors[0])
			} else {
				img.Set(x, y, opt.Colors[1])
			}
		}
	}

	// TODO: anti aliasing

	return img
}