// RandomMirror handler for "/labs/isogrids/random-mirror" // generates a random grid image symetric in the middle of the image. func RandomMirror(w http.ResponseWriter, r *http.Request) { size := extract.Size(r) colors := extract.Colors(r) prob := extract.Probability(r, 1/float64(len(colors))) write.ImageSVG(w) isogrids.RandomMirror(w, colors, size, prob) }
// Random handler for /isogrids/random. // Generates a random isogrid image. func Random(w http.ResponseWriter, r *http.Request) { colors := extract.Colors(r) prob := extract.Probability(r, 1/float64(len(colors))) size := extract.Size(r) lines := extract.Lines(r) write.ImageSVG(w) isogrids.Random(w, colors, size, size, lines, prob) }
// BannerRandomGradient handler for /isogrids/banner/random/gradient. // Generates a random gradient banner isogrid image. func BannerRandomGradient(w http.ResponseWriter, r *http.Request) { width := extract.Width(r) height := extract.Height(r) colors := extract.Colors(r) xt := extract.XTriangles(r) write.ImageSVG(w) isogrids.RandomGradient(w, colors, width, height, xt) }
// 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) colors := extract.Colors(r) prob := extract.Probability(r, 1/float64(len(colors))) xt := extract.XTriangles(r) write.ImageSVG(w) isogrids.Random(w, colors, width, height, xt, prob) }
// BannerGradient handler for "labs/squares/banner/gradient" // generates a color gradient random grid image. func BannerGradient(w http.ResponseWriter, r *http.Request) { width := extract.Width(r) height := extract.Height(r) xsquares := extract.XSquares(r) gv := extract.GradientVector(r, uint8(0), uint8(0), uint8(width), uint8(0)) gColors := extract.GColors(r) colors := extract.Colors(r) prob := extract.Probability(r, 1/float64(len(gColors))) write.ImageSVG(w) squares.RandomGradientColorSVG(w, colors, gColors, gv, width, height, xsquares, prob) }
// BannerRandomGradient handler for "/squares/banner/random/gradient" // generates a random banner grid image with gradient colors from brighter to darker color. func BannerRandomGradient(w http.ResponseWriter, r *http.Request) { width := extract.Width(r) height := extract.Height(r) xsquares := extract.XSquares(r) colors := extract.Colors(r) if f := extract.Format(r); f == format.JPEG { m := image.NewRGBA(image.Rect(0, 0, width, height)) squares.RandomGradientGrid(m, colors, xsquares) var img image.Image = m write.ImageJPEG(w, &img) } else if f == format.SVG { write.ImageSVG(w) squares.RandomGradientGridSVG(w, colors, width, height, xsquares) } }
// Random handler for "/squares/random" // generates a random 6 by 6 grid image. func Random(w http.ResponseWriter, r *http.Request) { size := extract.Size(r) colors := extract.Colors(r) prob := extract.Probability(r, 1/float64(len(colors))) if f := extract.Format(r); f == format.JPEG { m := image.NewRGBA(image.Rect(0, 0, size, size)) squares.RandomGrid(m, colors, 6, prob) var img image.Image = m write.ImageJPEG(w, &img) } else if f == format.SVG { write.ImageSVG(w) squares.RandomGridSVG(w, colors, size, size, 6, prob) } }
// 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) xsquares := extract.XSquares(r) colors := extract.Colors(r) prob := extract.Probability(r, 1/float64(len(colors))) if f := extract.Format(r); f == format.JPEG { m := image.NewRGBA(image.Rect(0, 0, width, height)) squares.RandomGrid(m, colors, xsquares, prob) var img image.Image = m write.ImageJPEG(w, &img) } else if f == format.SVG { write.ImageSVG(w) squares.RandomGridSVG(w, colors, width, height, xsquares, prob) } }
// Isogrids is the handler for /isogrids/:key // builds a 10x10 grid with alternate colors based on the string passed in the url. func Isogrids(w http.ResponseWriter, r *http.Request) { var err error var key string if key, err = route.Context.Get(r, "key"); err != nil { log.Println("Unable to get 'key' value: ", err) key = "" } h := md5.New() io.WriteString(h, key) key = fmt.Sprintf("%x", h.Sum(nil)[:]) colors := extract.Colors(r) size := extract.Size(r) lines := extract.Lines(r) write.ImageSVG(w) isogrids.Isogrids(w, key, colors, size, lines) }
// SpaceInvaders handler for /spaceinvaders/:key func SpaceInvaders(w http.ResponseWriter, r *http.Request) { var err error var key string if key, err = route.Context.Get(r, "key"); err != nil { log.Println("Unable to get 'key' value: ", err) key = "" } h := md5.New() io.WriteString(h, key) key = fmt.Sprintf("%x", h.Sum(nil)[:]) colors := extract.Colors(r) size := extract.Size(r) if Cache.IsCached(&w, r, key, colors, size) { w.WriteHeader(http.StatusNotModified) return } write.ImageSVG(w) spaceinvaders.SpaceInvaders(w, key, colors, size) }
// Hexa is the handler for /isogrids/hexa/:key // builds an hexagon with alternate colors. func Hexa(w http.ResponseWriter, r *http.Request) { var err error size := extract.Size(r) var key string if key, _ = route.Context.Get(r, "key"); err != nil { key = "" } h := md5.New() io.WriteString(h, key) key = fmt.Sprintf("%x", h.Sum(nil)[:]) lines := extract.Hexalines(r) colors := extract.Colors(r) if Cache.IsCached(&w, r, key, colors, size) { w.WriteHeader(http.StatusNotModified) return } write.ImageSVG(w) isogrids.Hexa(w, key, colors, size, lines) }
// Square handler for /squares/:key // builds a 6 by 6 grid with alternate colors based the key passed in the url. func Square(w http.ResponseWriter, r *http.Request) { var err error var key string if key, err = route.Context.Get(r, "key"); err != nil { log.Println("Unable to get 'key' value: ", err) key = "" } theme := extract.Theme(r) h := md5.New() io.WriteString(h, key) key = fmt.Sprintf("%x", h.Sum(nil)[:]) e := `"` + theme + 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 } } colors := extract.Colors(r) size := extract.Size(r) if f := extract.Format(r); f == format.JPEG { m := image.NewRGBA(image.Rect(0, 0, size, size)) squares.Image(m, key, colors) var img image.Image = m write.ImageJPEG(w, &img) } else if f == format.SVG { write.ImageSVG(w) squares.SVG(w, key, colors, size) } }