Commit b9fc2a4b authored by santiaago's avatar santiaago
Browse files

add functions to tools pkg.

+ RandomIndexFromArrayWithFreq
+ RandomColorFromArrayWithFreq

You can define the probability of the first color or index.
So if you call RandomColorFromArrayWithFreq(colors, 0.9)
This means the first color will be selected 90 out of 100
the rest of the times will be uniformly random between the rest of the colors. #71
parent ec501938
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -10,12 +10,26 @@ import (
	"strconv"
)

func RandomColorFromArrayWithFreq(colors []color.RGBA, prob float64) color.RGBA {
	if rf := rand.Float64(); rf < prob {
		return colors[0]
	}
	return RandomColorFromArray(colors[1:])
}

// RandomColorFromArray returns a random color from the given array.
func RandomColorFromArray(colors []color.RGBA) color.RGBA {
	r := rand.Intn(len(colors))
	return colors[r]
}

func RandomIndexFromArrayWithFreq(colors []color.RGBA, prob float64) int {
	if rf := rand.Float64(); rf < prob {
		return 0
	}
	return RandomIndexFromArray(colors[1:]) + 1
}

// RandomIndexFromArray returns an index from the given array.
func RandomIndexFromArray(colors []color.RGBA) int {
	r := rand.Intn(len(colors))