You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.3 KiB
63 lines
1.3 KiB
package effects
|
|
|
|
import (
|
|
"fmt"
|
|
"git.aiterp.net/lucifer3/server/device"
|
|
"math"
|
|
"strings"
|
|
)
|
|
|
|
func statesDescription(states []device.State) string {
|
|
sb := strings.Builder{}
|
|
sb.Grow(128)
|
|
|
|
sb.WriteRune('[')
|
|
for i, state := range states {
|
|
if i > 0 {
|
|
sb.WriteString(", ")
|
|
}
|
|
|
|
sb.WriteString(state.String())
|
|
}
|
|
sb.WriteRune(']')
|
|
|
|
return sb.String()
|
|
}
|
|
|
|
func gradientState(states []device.State, interpolate bool, index, length int) device.State {
|
|
indexFactor := math.Min(float64(index)/float64(length-1), 1)
|
|
|
|
return gradientStateFactor(states, interpolate, indexFactor)
|
|
}
|
|
|
|
func gradientStateFactor(states []device.State, interpolate bool, factor float64) device.State {
|
|
var stateIncrement float64
|
|
if interpolate {
|
|
stateIncrement = 1.0 / float64(len(states)-1)
|
|
} else {
|
|
stateIncrement = 1.0 / float64(len(states))
|
|
}
|
|
|
|
for i := range states {
|
|
a := float64(i) * stateIncrement
|
|
b := float64(i+1) * stateIncrement
|
|
|
|
if factor >= a && factor < b {
|
|
si := states[i]
|
|
if !interpolate || i+1 == len(states) {
|
|
return si
|
|
}
|
|
sj := states[i+1]
|
|
|
|
f := (factor - a) / stateIncrement
|
|
|
|
if f < 0 || f > 1 {
|
|
panic(fmt.Sprintf("assert(0 <= f <= 1) f = %.2f (if = %.2f, a = %.2f, si = %.2f)", f, factor, a, stateIncrement))
|
|
}
|
|
|
|
return si.Interpolate(sj, f)
|
|
}
|
|
}
|
|
|
|
return states[len(states)-1]
|
|
}
|