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

2 years ago
2 years ago
2 years ago
  1. package effects
  2. import (
  3. "fmt"
  4. "git.aiterp.net/lucifer3/server/device"
  5. "math"
  6. "strings"
  7. )
  8. func statesDescription(states []device.State) string {
  9. sb := strings.Builder{}
  10. sb.Grow(128)
  11. sb.WriteRune('[')
  12. for i, state := range states {
  13. if i > 0 {
  14. sb.WriteString(", ")
  15. }
  16. sb.WriteString(state.String())
  17. }
  18. sb.WriteRune(']')
  19. return sb.String()
  20. }
  21. func gradientState(states []device.State, interpolate bool, index, length int) device.State {
  22. indexFactor := math.Min(float64(index)/float64(length-1), 1)
  23. return gradientStateFactor(states, interpolate, indexFactor)
  24. }
  25. func gradientStateFactor(states []device.State, interpolate bool, factor float64) device.State {
  26. var stateIncrement float64
  27. if interpolate {
  28. stateIncrement = 1.0 / float64(len(states)-1)
  29. } else {
  30. stateIncrement = 1.0 / float64(len(states))
  31. }
  32. for i := range states {
  33. a := float64(i) * stateIncrement
  34. b := float64(i+1) * stateIncrement
  35. if factor >= a && factor < b {
  36. si := states[i]
  37. if !interpolate || i+1 == len(states) {
  38. return si
  39. }
  40. sj := states[i+1]
  41. f := (factor - a) / stateIncrement
  42. if f < 0 || f > 1 {
  43. panic(fmt.Sprintf("assert(0 <= f <= 1) f = %.2f (if = %.2f, a = %.2f, si = %.2f)", f, factor, a, stateIncrement))
  44. }
  45. return si.Interpolate(sj, f)
  46. }
  47. }
  48. return states[len(states)-1]
  49. }