Commit 9db67bc1 authored by santiaago's avatar santiaago
Browse files

delete dead code and rename some funcs

parent 691f01a8
Loading
Loading
Loading
Loading
+2 −58
Original line number Diff line number Diff line
@@ -97,62 +97,6 @@ func RandomGrid6X6SVG(w http.ResponseWriter, colors []color.RGBA, size int) {
	canvas.End()
}

// 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()
	squares := 6
	quad := size.X / squares
	middle := math.Ceil(float64(squares) / float64(2))
	colorMap := make(map[int]color.RGBA)
	var currentQuadrand = 0
	for x := 0; x < size.X; x++ {
		if x/quad != currentQuadrand {
			// when x quadrant changes, clear map
			colorMap = make(map[int]color.RGBA)
			currentQuadrand = x / quad
		}
		for y := 0; y < size.Y; y++ {
			yQuadrant := y / quad
			if _, ok := colorMap[yQuadrant]; !ok {
				if float64(yQuadrant) < middle {
					colorMap[yQuadrant] = randomColor(color1, color2)
				} else {
					colorMap[yQuadrant] = colorMap[squares-yQuadrant-1]
				}
			}
			m.Set(x, y, colorMap[yQuadrant])
		}
	}
}

// RandomSymetricInXGrid6X6 builds a grid image with with 2 colors selected at random for each quadrant.
func RandomSymetricInXGrid6X6(m *image.RGBA, color1, color2 color.RGBA) {
	size := m.Bounds().Size()
	squares := 6
	quad := size.X / squares
	middle := math.Ceil(float64(squares) / float64(2))
	colorMap := make(map[int]color.RGBA)
	var currentQuadrand = 0
	for y := 0; y < size.Y; y++ {
		if y/quad != currentQuadrand {
			// when y quadrant changes, clear map
			colorMap = make(map[int]color.RGBA)
			currentQuadrand = y / quad
		}
		for x := 0; x < size.X; x++ {
			xQuadrant := x / quad
			if _, ok := colorMap[xQuadrant]; !ok {
				if float64(xQuadrant) < middle {
					colorMap[xQuadrant] = randomColor(color1, color2)
				} else {
					colorMap[xQuadrant] = colorMap[squares-xQuadrant-1]
				}
			}
			m.Set(x, y, colorMap[xQuadrant])
		}
	}
}

func Squares(m *image.RGBA, key string, colors []color.RGBA) {
	size := m.Bounds().Size()
	squares := 6
@@ -171,7 +115,7 @@ func Squares(m *image.RGBA, key string, colors []color.RGBA) {
			xQuadrant := x / quad
			if _, ok := colorMap[xQuadrant]; !ok {
				if float64(xQuadrant) < middle {
					colorMap[xQuadrant] = colorFromKeyAndArray(key, colors, xQuadrant+3*yQuadrant)
					colorMap[xQuadrant] = pickColor(key, colors, xQuadrant+3*yQuadrant)
				} else if xQuadrant < squares {
					colorMap[xQuadrant] = colorMap[squares-xQuadrant-1]
				} else {
@@ -199,7 +143,7 @@ func SquaresSVG(w http.ResponseWriter, key string, colors []color.RGBA, size int
			x := xQ * quadrantSize
			if _, ok := colorMap[xQ]; !ok {
				if float64(xQ) < middle {
					colorMap[xQ] = colorFromKeyAndArray(key, colors, xQ+3*yQ)
					colorMap[xQ] = pickColor(key, colors, xQ+3*yQ)
				} else if xQ < squares {
					colorMap[xQ] = colorMap[squares-xQ-1]
				} else {
+4 −4
Original line number Diff line number Diff line
@@ -157,7 +157,7 @@ func Isogrids(w http.ResponseWriter, key string, colors []color.RGBA, size int)
			}
			xs := []int{x1, x2, x3}
			ys := []int{y1, y2, y3}
			fill1 := fillFromRGBA(colorFromKeyAndArray(key, colors, (xL+3*yL+lines)%15))
			fill1 := fillFromRGBA(pickColor(key, colors, (xL+3*yL+lines)%15))
			canvas.Polygon(xs, ys, fill1)
			var x11, x12, x13, y11, y12, y13 int
			if (xL % 2) == 0 {
@@ -177,7 +177,7 @@ func Isogrids(w http.ResponseWriter, key string, colors []color.RGBA, size int)
			}
			xs1 := []int{x11, x12, x13}
			ys1 := []int{y11, y12, y13}
			fill2 := fillFromRGBA(colorFromKeyAndArray(key, colors, (xL+3*yL+1+lines)%15))
			fill2 := fillFromRGBA(pickColor(key, colors, (xL+3*yL+1+lines)%15))
			canvas.Polygon(xs1, ys1, fill2)
			// apply mirror:
			xs[0] = (lines * fringeSize) - xs[0]
@@ -207,10 +207,10 @@ func IsogridsHexa(w http.ResponseWriter, key string, colors []color.RGBA, size,
			fill1 := fillWhite()
			fill2 := fillWhite()
			if isFill1InHexagon(xL, yL, lines) {
				fill1 = fillFromRGBA(colorFromKeyAndArray(key, colors, (xL+3*yL+lines)%15))
				fill1 = fillFromRGBA(pickColor(key, colors, (xL+3*yL+lines)%15))
			}
			if isFill2InHexagon(xL, yL, lines) {
				fill2 = fillFromRGBA(colorFromKeyAndArray(key, colors, (xL+3*yL+1+lines)%15))
				fill2 = fillFromRGBA(pickColor(key, colors, (xL+3*yL+1+lines)%15))
			}

			if !isFill1InHexagon(xL, yL, lines) && !isFill2InHexagon(xL, yL, lines) {
+8 −24
Original line number Diff line number Diff line
@@ -9,39 +9,23 @@ import (
	"strconv"
)

// randomColor 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
}

// getRandomColor returns a random color between c1 and c2
// randomColorFromArray returns a random color from the given array.
func randomColorFromArray(colors []color.RGBA) color.RGBA {
	r := rand.Intn(len(colors))
	return colors[r]
}

// return a fill SVG style from color.RGBA
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
}

func colorFromKeyAndArray(key string, colors []color.RGBA, index int) color.RGBA {
// pickColor returns a color given a key string, an array of colors and an index.
// key: should be a md5 hash string.
// index: is an index from the key string. Should be in interval [0, 16]
// Algorithm: pickColor converts the key[index] value to a decimal value.
// We pick the ith colors that respects the equality value%numberOfColors == i.
func pickColor(key string, colors []color.RGBA, index int) color.RGBA {
	n := len(colors)
	s := hex.EncodeToString([]byte{key[index]})
	if r, err := strconv.ParseInt(s, 16, 0); err == nil {