Commit 7a9f205f authored by santiaago's avatar santiaago
Browse files

add isogrid handlers

parent 2a7fc1fb
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import (
	"flag"
	"github.com/taironas/route"
	"github.com/taironas/tinygraphs/controllers/checkerboard"
	"github.com/taironas/tinygraphs/controllers/isogrids"
	"github.com/taironas/tinygraphs/controllers/squares"
	"log"
	"net/http"
@@ -24,6 +25,8 @@ func main() {
	r.HandleFunc("/squares/[a-zA-Z0-9\\.]+/?", squares.Square)      //cached
	r.HandleFunc("/squares/[0-8]/[a-zA-Z0-9\\.]+/?", squares.Color) // cached

	r.HandleFunc("/isogrids/[a-zA-Z0-9]+/?", isogrids.Isogrids)

	r.AddStaticResource(root)

	log.Println("Listening on " + os.Getenv("PORT"))
+55 −0
Original line number Diff line number Diff line
package isogrids

import (
	"crypto/md5"
	"fmt"
	tgColors "github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw"
	"github.com/taironas/tinygraphs/extract"
	// "github.com/taironas/tinygraphs/format"
	"github.com/taironas/tinygraphs/misc"
	"github.com/taironas/tinygraphs/write"
	"io"
	"log"
	"net/http"
	//	"strings"
)

// Isogrids is the handler for /isogrids/[a-zA-Z0-9]+/?.
// builds a 10x10 grid with alternate colors based on the string passed in the url.
func Isogrids(w http.ResponseWriter, r *http.Request) {

	if id, err := misc.PermalinkString(r, 2); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		h := md5.New()
		io.WriteString(h, id)
		key := fmt.Sprintf("%x", h.Sum(nil)[:])

		// e := `"` + key + `"`
		// w.Header().Set("Etag", e)
		// w.Header().Set("Cache-Control", "max-age=2592000") // 30 days

		// if match := r.Header.Get("If-None-Match"); match != "" {
		// 	if strings.Contains(match, e) {
		// 		w.WriteHeader(http.StatusNotModified)
		// 		return
		// 	}
		// }

		colorMap := tgColors.MapOfColorPatterns()
		bg, err1 := extract.Background(r)
		if err1 != nil {
			bg = colorMap[0][0]
		}
		fg, err2 := extract.Foreground(r)
		if err2 != nil {
			fg = colorMap[0][1]
		}
		size := extract.Size(r)
		// if f := extract.Format(r); f == format.SVG {
		write.ImageSVG(w)
		draw.IsogridsSVG(w, key, bg, fg, size)
		// }
	}
}
+2 −2
Original line number Diff line number Diff line
@@ -17,8 +17,8 @@ import (
	"strings"
)

// Square is the handler for /squares/[A-Za-z0-9]+/?
// build a 6x6 grid with alternate colors based on the number passed in the url
// Square is the handler for /squares/[A-Za-z0-9]+/?.
// builds a 6x6 grid with alternate colors based on the number passed in the url.
func Square(w http.ResponseWriter, r *http.Request) {

	if id, err := misc.PermalinkString(r, 2); err != nil {
+0 −31
Original line number Diff line number Diff line
package draw

import (
	"encoding/hex"
	"fmt"
	"github.com/ajstarks/svgo"
	"image"
	"image/color"
	"log"
	"math"
	"math/rand"
	"net/http"
	"strconv"
)

//Grid6X6 builds an image with 6X6 quadrants of alternate colors.
@@ -101,15 +96,6 @@ func RandomGrid6X6SVG(w http.ResponseWriter, color1, color2 color.RGBA, size int
	canvas.End()
}

// getRandomColor returns a random color between c1 and c2
func randomColor(c1, c2 color.RGBA) color.RGBA {
	r := rand.Intn(2)
	if r == 1 {
		return c1
	}
	return c2
}

// RandomSymetricInYGrid6X6 builds a grid image with with 2 colors selected at random for each quadrant.
func RandomSymetricInYGrid6X6(m *image.RGBA, color1, color2 color.RGBA) {
	size := m.Bounds().Size()
@@ -224,20 +210,3 @@ func SquaresSVG(w http.ResponseWriter, key string, color1, color2 color.RGBA, si
	}
	canvas.End()
}

func fillFromRGBA(c color.RGBA) string {
	return fmt.Sprintf("fill:rgb(%d,%d,%d)", c.R, c.G, c.B)
}

func colorFromKey(key string, color1, color2 color.RGBA, index int) color.RGBA {
	s := hex.EncodeToString([]byte{key[index]})
	if r, err := strconv.ParseInt(s, 16, 0); err == nil {
		if r%2 == 0 {
			return color1
		}
		return color2
	} else {
		log.Println("Error calling ParseInt(%v, 16, 0)", s, err)
	}
	return color1
}

draw/isogrids.go

0 → 100644
+38 −0
Original line number Diff line number Diff line
package draw

import (
	"github.com/ajstarks/svgo"
	"image/color"
	"math"
	"net/http"
)

// IsogridsSVG builds an image with 10x10 grids of alternate colors.
func IsogridsSVG(w http.ResponseWriter, key string, color1, color2 color.RGBA, size int) {
	canvas := svg.New(w)
	canvas.Start(size, size)

	squares := 6
	quadrantSize := size / squares
	middle := math.Ceil(float64(squares) / float64(2))
	colorMap := make(map[int]color.RGBA)
	for yQ := 0; yQ < squares; yQ++ {
		y := yQ * quadrantSize
		colorMap = make(map[int]color.RGBA)

		for xQ := 0; xQ < squares; xQ++ {
			x := xQ * quadrantSize
			if _, ok := colorMap[xQ]; !ok {
				if float64(xQ) < middle {
					colorMap[xQ] = colorFromKey(key, color1, color2, xQ+3*yQ)
				} else if xQ < squares {
					colorMap[xQ] = colorMap[squares-xQ-1]
				} else {
					colorMap[xQ] = colorMap[0]
				}
			}
			canvas.Rect(x, y, quadrantSize, quadrantSize, fillFromRGBA(colorMap[xQ]))
		}
	}
	canvas.End()
}
Loading