~whereswaldon/chipalee

56b5a1a49a5cc79ecca22db9badd483e746411a2 — Chris Waldon 1 year, 9 months ago 22e42e0
main: improve organ coloration

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
1 files changed, 58 insertions(+), 9 deletions(-)

M main.go
M main.go => main.go +58 -9
@@ 622,18 622,13 @@ func run(w *app.Window) error {
			ready = nil
			audioInit = true
			for idx, f := range freqs {
				hue := float64(idx) / float64(IdxMax) * 360
				col := colorful.Hcl(hue, 1, 1).Clamped()
				r, g, b, a := col.RGBA()
				pos := float64(idx) / float64(IdxMax)
				col := gradient.GetInterpolatedColorFor(pos)
				nrgba := color.NRGBAModel.Convert(col).(color.NRGBA)
				players[idx] = Pipe{
					player: c.NewPlayer(NewSineWave(f)),
					freq:   f,
					col: color.NRGBA{
						R: uint8(r),
						G: uint8(g),
						B: uint8(b),
						A: uint8(a),
					},
					col:    nrgba,
				}
			}
			log.Println("organ initialized")


@@ 653,3 648,57 @@ func main() {
	}()
	app.Main()
}

/* Start copied from go-colorful example, this part is MIT licensed */
// https://github.com/lucasb-eyer/go-colorful/blob/master/doc/gradientgen/gradientgen.go

// This table contains the "keypoints" of the colorgradient you want to generate.
// The position of each keypoint has to live in the range [0,1]
type GradientTable []struct {
	Col colorful.Color
	Pos float64
}

// This is the meat of the gradient computation. It returns a HCL-blend between
// the two colors around `t`.
// Note: It relies heavily on the fact that the gradient keypoints are sorted.
func (gt GradientTable) GetInterpolatedColorFor(t float64) colorful.Color {
	for i := 0; i < len(gt)-1; i++ {
		c1 := gt[i]
		c2 := gt[i+1]
		if c1.Pos <= t && t <= c2.Pos {
			// We are in between c1 and c2. Go blend them!
			t := (t - c1.Pos) / (c2.Pos - c1.Pos)
			return c1.Col.BlendHcl(c2.Col, t).Clamped()
		}
	}

	// Nothing found? Means we're at (or past) the last gradient keypoint.
	return gt[len(gt)-1].Col
}

// This is a very nice thing Golang forces you to do!
// It is necessary so that we can write out the literal of the colortable below.
func MustParseHex(s string) colorful.Color {
	c, err := colorful.Hex(s)
	if err != nil {
		panic("MustParseHex: " + err.Error())
	}
	return c
}

var gradient = GradientTable{
	{MustParseHex("#9e0142"), 0.0},
	{MustParseHex("#d53e4f"), 0.1},
	{MustParseHex("#f46d43"), 0.2},
	{MustParseHex("#fdae61"), 0.3},
	{MustParseHex("#fee090"), 0.4},
	{MustParseHex("#ffffbf"), 0.5},
	{MustParseHex("#e6f598"), 0.6},
	{MustParseHex("#abdda4"), 0.7},
	{MustParseHex("#66c2a5"), 0.8},
	{MustParseHex("#3288bd"), 0.9},
	{MustParseHex("#5e4fa2"), 1.0},
}

/* end copied from go-colorful */