Exemple #1
0
// Theme handler builds an image with the colors of a theme
// the theme is defined by keyword :theme
// url: "/themes/:theme"
func Theme(w http.ResponseWriter, r *http.Request) {
	var err error
	var th string
	if th, _ = route.Context.Get(r, "theme"); err != nil {
		th = "base"
	}

	colorMap := colors.MapOfColorThemes()
	var theme []color.RGBA
	if val, ok := colorMap[th]; ok {
		theme = val
	} else {
		theme = colorMap["base"]
	}

	width := extract.WidthOrDefault(r, 20*len(theme))
	height := extract.HeightOrDefault(r, 34)

	if f := extract.Format(r); f == format.JPEG {
		m := image.NewRGBA(image.Rect(0, 0, width, height))
		squares.Palette(m, theme)
		var img image.Image = m
		write.ImageJPEG(w, &img)
	} else if f == format.SVG {
		write.ImageSVG(w)
		squares.PaletteSVG(w, theme, width, height)
	}
}
Exemple #2
0
// HalfDiagonals is the handler for /isogrids/labs/halfdiagonals
// builds a half diagonal (each diagonal goes to the middle of the square) grid.
func HalfDiagonals(w http.ResponseWriter, r *http.Request) {

	bg, fg := extract.ExtraColors(r)
	size := extract.Size(r)
	write.ImageSVG(w)
	isogrids.HalfDiagonals(w, "", bg, fg, size)
}
Exemple #3
0
// 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)
}
Exemple #4
0
// 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)
}
Exemple #5
0
// 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)
}
Exemple #6
0
// 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)
}
Exemple #7
0
// 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)
}
Exemple #8
0
// 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)
	}
}
Exemple #9
0
// 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)
	}
}
Exemple #10
0
// 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)
	}
}
Exemple #11
0
// 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)
}
Exemple #12
0
// 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)
}
Exemple #13
0
// 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)
}
Exemple #14
0
// Checkerboard is the handler for /checkerboard
// build a 6x6 checkerboard with alternate black and white colors.
func Checkerboard(w http.ResponseWriter, r *http.Request) {
	size := extract.Size(r)
	theme := extract.Theme(r)
	colorMap := colors.MapOfColorThemes()

	var c1, c2 color.RGBA
	if val, ok := colorMap[theme]; ok {
		c1 = val[0]
		c2 = val[1]
	} else {
		c1 = colorMap["base"][0]
		c2 = colorMap["base"][1]
	}
	if f := extract.Format(r); f == format.JPEG {
		m := image.NewRGBA(image.Rect(0, 0, size, size))
		squares.Grid(m, c1, c2)
		var img image.Image = m
		write.ImageJPEG(w, &img)
	} else if f == format.SVG {
		write.ImageSVG(w)
		squares.GridSVG(w, c1, c2, size)
	}
}
Exemple #15
0
// 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)
	}
}