Commit d111d71c authored by santiaago's avatar santiaago
Browse files

clean up gradient code

parent 0eb98438
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -37,7 +37,6 @@ func main() {
	r.HandleFunc("/themes/:theme", themes.Theme)
	r.HandleFunc("/labs/checkerboard", checkerboard.Checkerboard)
	r.HandleFunc("/labs/squares/random", squares.Random)
	r.HandleFunc("/labs/squares/gradient/:key", squares.Gradient)
	r.HandleFunc("/labs/squares/banner/gradient", squares.BannerGradient)
	r.HandleFunc("/labs/isogrids/hexa", isogrids.Hexa)
	r.HandleFunc("/labs/isogrids/hexa/:key", isogrids.Hexa)
+0 −42
Original line number Diff line number Diff line
package squares

import (
	"crypto/md5"
	"fmt"
	"image/color"
	"io"
	"log"
	"net/http"

	"github.com/taironas/route"
	"github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw/squares"
	"github.com/taironas/tinygraphs/extract"
	"github.com/taironas/tinygraphs/write"
)

// Gradient handler for "/labs/squares/gradient/:key"
// generates a color gradient random grid image.
func Gradient(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)[:])

	numColors := extract.NumColors(r)
	colorMap := colors.MapOfColorThemes()

	bg, fg := extract.ExtraColors(r, colorMap)

	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)
	}

	size := extract.Size(r)
	write.ImageSVG(w)
	squares.GradientSVG(w, key, colors, size, size, 6)
}

// Gradient handler for "labs/squares/banner/gradient"
// generates a color gradient random grid image.
func BannerGradient(w http.ResponseWriter, r *http.Request) {
+0 −56
Original line number Diff line number Diff line
@@ -11,62 +11,6 @@ import (
	"github.com/taironas/tinygraphs/draw"
)

// GradientSVG builds an image with 6 by 6 quadrants of alternate colors.
func GradientSVG(w http.ResponseWriter, key string, colors []color.RGBA, width, height, xsquares int) {

	var gradientColors []svg.Offcolor
	gradientColors = make([]svg.Offcolor, len(colors))
	percentage := uint8(100 / len(colors))

	step := uint8(100 / len(colors))
	for i, c := range colors {
		gradientColors[i] = svg.Offcolor{percentage, RGBToHex(c.R, c.G, c.B), 1}
		percentage += step
	}

	canvas := svg.New(w)
	canvas.Start(width, height)
	canvas.Def()
	canvas.LinearGradient("gradientColors", 0, 0, uint8(width), 0, gradientColors)
	canvas.DefEnd()
	canvas.Rect(0, 0, width, height, "fill:url(#gradientColors)")

	squares := xsquares
	quadrantSize := width / squares
	middle := math.Ceil(float64(squares) / float64(2))
	colorMap := make(map[int]color.RGBA)
	colorIndex := make(map[int]int)
	for yQ := 0; yQ < squares; yQ++ {
		y := yQ * quadrantSize
		colorMap = make(map[int]color.RGBA)
		colorIndex = make(map[int]int)
		for xQ := 0; xQ < squares; xQ++ {
			x := xQ * quadrantSize
			fill := ""
			if _, ok := colorMap[xQ]; !ok {
				if float64(xQ) < middle {
					colorIndex[xQ] = draw.PickIndex(key, len(colors), xQ+3*yQ)
					colorMap[xQ] = draw.PickColor(key, colors, xQ+3*yQ)
				} else if xQ < squares {
					colorIndex[xQ] = colorIndex[squares-xQ-1]
					colorMap[xQ] = colorMap[squares-xQ-1]
				} else {
					colorIndex[xQ] = colorIndex[0]
					colorMap[xQ] = colorMap[0]
				}
			}
			if colorIndex[xQ] != 0 {
				fill = draw.FillFromRGBA(colorMap[xQ])
			} else {
				fill = "fill:none"

			}
			canvas.Rect(x, y, quadrantSize, quadrantSize, fill)
		}
	}
	canvas.End()
}

// RandomGradientSVG builds an image.
func RandomGradientSVG(w http.ResponseWriter, colors, gColors []color.RGBA, gv colors.GradientVector, width, height, xsquares int) {