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
// 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 #3
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 #4
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 #5
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 #6
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)
	}
}