Commit 918103ba authored by santiaago's avatar santiaago
Browse files

first take at square random banners

parent aafea484
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ func main() {
	r := new(route.Router)

	r.HandleFunc("/squares", squares.Random)
	r.HandleFunc("/squares/banner/random", squares.BannerRandom)
	r.HandleFunc("/squares/:key", squares.Square) //cached
	r.HandleFunc("/isogrids/:key", isogrids.Isogrids)
	r.HandleFunc("/spaceinvaders/:key", spaceinvaders.SpaceInvaders)
+48 −0
Original line number Diff line number Diff line
package squares

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

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

// BannerRandom handler for "/squares/banner/random"
// generates a random banner grid image.
func BannerRandom(w http.ResponseWriter, r *http.Request) {
	width := extract.Width(r)
	height := extract.Height(r)

	numColors := extract.NumColors(r)

	colorMap := colors.MapOfColorThemes()

	bg, fg := extract.ExtraColors(r, colorMap)

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

	if f := extract.Format(r); f == format.JPEG {
		m := image.NewRGBA(image.Rect(0, 0, width, height))
		squares.RandomGrid(m, colors, 50)
		var img image.Image = m
		write.ImageJPEG(w, &img)
	} else if f == format.SVG {
		write.ImageSVG(w)
		squares.RandomGridSVG(w, colors, width, height, 50)
	}
}
+2 −2
Original line number Diff line number Diff line
@@ -36,11 +36,11 @@ func Random(w http.ResponseWriter, r *http.Request) {

	if f := extract.Format(r); f == format.JPEG {
		m := image.NewRGBA(image.Rect(0, 0, size, size))
		squares.RandomGrid(m, colors)
		squares.RandomGrid(m, colors, 6)
		var img image.Image = m
		write.ImageJPEG(w, &img)
	} else if f == format.SVG {
		write.ImageSVG(w)
		squares.RandomGridSVG(w, colors, size)
		squares.RandomGridSVG(w, colors, size, size, 6)
	}
}
+6 −6
Original line number Diff line number Diff line
@@ -55,9 +55,9 @@ func GridSVG(w http.ResponseWriter, color1, color2 color.RGBA, size int) {
}

// RandomGrid builds a 6 by 6 grid image with with 2 colors selected at random for each quadrant.
func RandomGrid(m *image.RGBA, colors []color.RGBA) {
func RandomGrid(m *image.RGBA, colors []color.RGBA, xSquares int) {
	size := m.Bounds().Size()
	quad := size.X / 6
	quad := size.X / xSquares
	colorMap := make(map[int]color.RGBA)
	var currentQuadrand = 0
	for x := 0; x < size.X; x++ {
@@ -77,11 +77,11 @@ func RandomGrid(m *image.RGBA, colors []color.RGBA) {
}

// RandomGrid6X6SVG builds a grid image with with 2 colors selected at random for each quadrant.
func RandomGridSVG(w http.ResponseWriter, colors []color.RGBA, size int) {
func RandomGridSVG(w http.ResponseWriter, colors []color.RGBA, width, height, xSquares int) {
	canvas := svg.New(w)
	canvas.Start(size, size)
	squares := 6
	quadrantSize := size / squares
	canvas.Start(width, height)
	squares := xSquares
	quadrantSize := width / squares
	colorMap := make(map[int]color.RGBA)
	for yQ := 0; yQ < squares; yQ++ {
		y := yQ * quadrantSize
+30 −1
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ func Size(r *http.Request) int {
		if size, errSize := strconv.ParseInt(strSize, 0, 64); errSize == nil {
			isize := int(size)
			if isize > 0 && isize < 1000 {
				return int(size)
				return isize
			}
		}
	}
@@ -144,3 +144,32 @@ func Lines(r *http.Request) int {
	}
	return 6
}

// Width returns the value of the 'w' parameter in the http.Request.
func Width(r *http.Request) int {
	strW := r.FormValue("w")
	if len(strW) > 0 {
		if w, errW := strconv.ParseInt(strW, 0, 64); errW == nil {
			iw := int(w)
			if iw > 0 {
				return iw
			}
		}
	}
	return 720
}

// Height returns the value of the 'h' parameter in the http.Request.
func Height(r *http.Request) int {
	strH := r.FormValue("h")
	if len(strH) > 0 {
		if h, errH := strconv.ParseInt(strH, 0, 64); errH == nil {
			ih := int(h)
			if ih > 0 {
				return ih
			}
		}
	}
	return 300

}