Commit 5a193925 authored by santiaago's avatar santiaago
Browse files

add random isogrids banner #36

parent b9def0d7
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -26,6 +26,8 @@ func main() {
	r.HandleFunc("/squares/banner/random", squares.BannerRandom)
	r.HandleFunc("/squares/banner/random/gradient", squares.BannerRandomGradient)
	r.HandleFunc("/squares/:key", squares.Square) //cached

	r.HandleFunc("/isogrids/banner/random", isogrids.BannerRandom)
	r.HandleFunc("/isogrids/:key", isogrids.Isogrids)
	r.HandleFunc("/spaceinvaders/:key", spaceinvaders.SpaceInvaders)

+42 −0
Original line number Diff line number Diff line
package isogrids

import (
	"image/color"
	"net/http"

	"github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw/isogrids"
	"github.com/taironas/tinygraphs/extract"
	"github.com/taironas/tinygraphs/write"
)

// BannerRandom handler for /isogrids/banner/random.
// Generates a random banner isogrid image.
func BannerRandom(w http.ResponseWriter, r *http.Request) {
	width := extract.Width(r)
	height := extract.Height(r)

	colorMap := colors.MapOfColorThemes()
	bg, fg := extract.ExtraColors(r, colorMap)
	theme := extract.Theme(r)
	if val, ok := colorMap[theme]; ok {
		bg = val[0]
		fg = val[1]
	}

	var colors []color.RGBA
	if theme != "base" {
		if _, ok := colorMap[theme]; ok {
			numColors := extract.NumColors(r)
			colors = append(colors, colorMap[theme][0:numColors]...)
		} else {
			colors = append(colors, colorMap["base"]...)
		}
	} else {
		colors = append(colors, bg, fg)
	}

	xt := extract.XTriangles(r)
	write.ImageSVG(w)
	isogrids.Random(w, "", colors, width, height, xt)
}
+1 −1
Original line number Diff line number Diff line
@@ -37,5 +37,5 @@ func Random(w http.ResponseWriter, r *http.Request) {
	size := extract.Size(r)
	lines := extract.Lines(r)
	write.ImageSVG(w)
	isogrids.Random(w, "", colors, size, lines)
	isogrids.Random(w, "", colors, size, size, lines)
}
+3 −3
Original line number Diff line number Diff line
@@ -10,11 +10,11 @@ import (
)

// Random creates an isogrids svg image with half diagonals.
func Random(w http.ResponseWriter, key string, colors []color.RGBA, size, lines int) {
func Random(w http.ResponseWriter, key string, colors []color.RGBA, width, height, lines int) {
	canvas := svg.New(w)
	canvas.Start(size, size)
	canvas.Start(width, height)

	fringeSize := size / lines
	fringeSize := width / lines

	// triangle grid here:
	for xL := -1; xL <= lines; xL++ {
+15 −0
Original line number Diff line number Diff line
@@ -187,3 +187,18 @@ func XSquares(r *http.Request) int {
	}
	return 50
}

// XTriangles returns the value of the 'xt' parameter in the http.Request.
// Used to defined the number of triangles that are wanted in the x axis of an image
func XTriangles(r *http.Request) int {
	strXT := r.FormValue("xt")
	if len(strXT) > 0 {
		if xt, errXT := strconv.ParseInt(strXT, 0, 64); errXT == nil {
			ixt := int(xt)
			if ixt > 0 {
				return ixt
			}
		}
	}
	return 50
}