Example #1
0
// RandomGradient creates an isogrids svg image with half diagonals.
// colors are filled at random in the image with a frequency that decreases
// from left to right.
func RandomGradient(w http.ResponseWriter, colors []color.RGBA, width, height, lines int) {
	canvas := svg.New(w)
	canvas.Start(width, height)

	fringeSize := width / lines
	distance := distanceTo3rdPoint(fringeSize)
	fringeSize = distance
	lines = width / fringeSize

	for xL := 0; xL < lines; xL++ {
		percentage := 100 - int(float64(xL)/float64(lines)*100)
		for yL := -1; yL < lines; yL++ {
			var x1, x2, y1, y2, y3 int
			if (xL % 2) == 0 {
				x1, y1, x2, y2, _, y3 = right1stTriangle(xL, yL, fringeSize, distance)
			} else {
				x1, y1, x2, y2, _, y3 = left1stTriangle(xL, yL, fringeSize, distance)
			}
			xs := []int{x2, x1, x2}
			ys := []int{y1, y2, y3}
			canvas.Polygon(xs, ys, draw.FillFromRGBA(draw.ColorByPercentage(colors, percentage)))

			var x11, x12, y11, y12, y13 int
			if (xL % 2) == 0 {
				x11, y11, x12, y12, _, y13 = left2ndTriangle(xL, yL, fringeSize, distance)

				// we make sure that the previous triangle and this one touch each other in this point.
				y12 = y3
			} else {
				x11, y11, x12, y12, _, y13 = right2ndTriangle(xL, yL, fringeSize, distance)

				// in order to have a perfect hexagon,
				// we make sure that the previous triangle and this one touch each other in this point.
				y12 = y1 + fringeSize
			}
			xs1 := []int{x12, x11, x12}
			ys1 := []int{y11, y12, y13}
			canvas.Polygon(xs1, ys1, draw.FillFromRGBA(draw.ColorByPercentage(colors, percentage)))
		}
	}
	canvas.End()
}
Example #2
0
// RandomGradientGridSVG builds a grid image with with x colors selected at random for each quadrant.
func RandomGradientGridSVG(w http.ResponseWriter, colors []color.RGBA, width, height, xSquares int) {
	canvas := svg.New(w)
	canvas.Start(width, height)
	squares := xSquares
	quadrantSize := width / squares
	colorMap := make(map[int]color.RGBA)
	for yQ := 0; yQ < squares; yQ++ {
		y := yQ * quadrantSize
		colorMap = make(map[int]color.RGBA)

		for xQ := 0; xQ < squares; xQ++ {
			x := xQ * quadrantSize
			if _, ok := colorMap[xQ]; !ok {
				percentage := 100 - int(float64(xQ)/float64(squares)*100)
				colorMap[xQ] = draw.ColorByPercentage(colors, percentage)
			}
			canvas.Rect(x, y, quadrantSize, quadrantSize, draw.FillFromRGBA(colorMap[xQ]))
		}
	}
	canvas.End()
}
Example #3
0
// RandomGradientGrid builds a grid image with with x colors selected at random for each quadrant going from brighter to dracker color.
func RandomGradientGrid(m *image.RGBA, colors []color.RGBA, xSquares int) {
	size := m.Bounds().Size()
	quad := size.X / xSquares
	colorMap := make(map[int]color.RGBA)
	var currentQuadrand = 0
	for x := 0; x < size.X; x++ {
		if x/quad != currentQuadrand {

			colorMap = make(map[int]color.RGBA)
			currentQuadrand = x / quad
		}
		percentage := 100 - int(float64(x)/float64(size.X)*100)
		for y := 0; y < size.Y; y++ {
			yQuadrant := y / quad
			if _, ok := colorMap[yQuadrant]; !ok {
				colorMap[yQuadrant] = draw.ColorByPercentage(colors, percentage)
			}
			m.Set(x, y, colorMap[yQuadrant])
		}
	}
}