Commit 985e5106 authored by santiaago's avatar santiaago
Browse files

isogrids/random now supports themes and numcolors parameters #18

parent d5f0eaca
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -27,6 +27,9 @@ func main() {
	r.HandleFunc("/squares/random", squares.Random)
	r.HandleFunc("/squares/:key", squares.Square) //cached

	r.HandleFunc("/isogrids/random", isogrids.Random)
	r.HandleFunc("/isogrids/random-mirror", isogrids.RandomMirror)

	r.HandleFunc("/isogrids/:key", isogrids.Isogrids)

	r.HandleFunc("/isogrids/labs/skeleton", isogrids.Skeleton)
@@ -35,9 +38,6 @@ func main() {
	r.HandleFunc("/isogrids/labs/gridbw", isogrids.GridBW)
	r.HandleFunc("/isogrids/labs/grid2colors", isogrids.Grid2Colors)

	r.HandleFunc("/isogrids/random", isogrids.Random)
	r.HandleFunc("/isogrids/random-mirror", isogrids.RandomMirror)

	r.AddStaticResource(root)

	log.Println("Listening on " + os.Getenv("PORT"))
+18 −3
Original line number Diff line number Diff line
@@ -10,11 +10,16 @@ import (
	"github.com/taironas/tinygraphs/write"
)

// Random handler for /isogrids/random.
// Generates a random isogrid image.
func Random(w http.ResponseWriter, r *http.Request) {

	colorMap := colors.MapOfColorThemes()
	var err error

	colorMap := colors.MapOfColorThemes()
	size := extract.Size(r)
	theme := extract.Theme(r)
	numColors := extract.NumColors(r)

	var bg, fg color.RGBA
	if bg, err = extract.Background(r); err != nil {
@@ -29,7 +34,17 @@ func Random(w http.ResponseWriter, r *http.Request) {
		fg = val[1]
	}

	size := extract.Size(r)
	var colors []color.RGBA
	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)
	}

	write.ImageSVG(w)
	draw.IsogridsRandom(w, "", bg, fg, size)
	draw.IsogridsRandom(w, "", colors, size)
}
+4 −4
Original line number Diff line number Diff line
@@ -8,8 +8,8 @@ import (
	"github.com/ajstarks/svgo"
)

// Isogrids builds an image with 10x10 grids of half diagonals
func IsogridsRandom(w http.ResponseWriter, key string, color1, color2 color.RGBA, size int) {
// IsogridsRandom creates an isogrids image with half diagonals.
func IsogridsRandom(w http.ResponseWriter, key string, colors []color.RGBA, size int) {
	canvas := svg.New(w)
	canvas.Start(size, size)

@@ -37,7 +37,7 @@ func IsogridsRandom(w http.ResponseWriter, key string, color1, color2 color.RGBA
			}
			xs := []int{x1, x2, x3}
			ys := []int{y1, y2, y3}
			canvas.Polygon(xs, ys, fmt.Sprintf("stroke:black;stroke-width:2; %s", fillFromRGBA(randomColor(color1, color2))))
			canvas.Polygon(xs, ys, fmt.Sprintf("stroke:black;stroke-width:2; %s", fillFromRGBA(randomColorFromArray(colors))))

			var x11, x12, x13, y11, y12, y13 int
			if (xL % 2) == 0 {
@@ -57,7 +57,7 @@ func IsogridsRandom(w http.ResponseWriter, key string, color1, color2 color.RGBA
			}
			xs1 := []int{x11, x12, x13}
			ys1 := []int{y11, y12, y13}
			canvas.Polygon(xs1, ys1, fmt.Sprintf("stroke:black;stroke-width:2; %s", fillFromRGBA(randomColor(color1, color2))))
			canvas.Polygon(xs1, ys1, fmt.Sprintf("stroke:black;stroke-width:2; %s", fillFromRGBA(randomColorFromArray(colors))))
		}
	}
	canvas.End()