|
- import time
-
- from gradients import Gradient
-
- class Pattern:
- def __init__(self, gradient, cycle_length, cycle_time_s=None, reverse=False, march=False):
- self.gradient = gradient
- self.cycle_length = cycle_length
- self.cycle_time_s = cycle_time_s or (cycle_length / 2 if march else cycle_length)
- self.reverse = reverse
- self.march = march
- self.last = None
- self.offset = 0
-
- def step(self, length):
- now = time.time()
- duration = 0 if self.last is None else now - self.last
- self.last = now
-
- offset_delta = self.cycle_length * duration / self.cycle_time_s
- self.offset = (self.offset + (offset_delta if self.reverse else -offset_delta)) % self.cycle_length
-
- offset = self.offset if not self.march else int(self.offset)
- return (self.gradient.at(((i + offset) / self.cycle_length + 1) % 1) for i in range(0, length))
-
- def from_dict(dict):
- return Pattern(
- Gradient.from_dict(dict['gradient']),
- dict['cycle_length'],
- cycle_time_s = dict.get('cycle_time_s'),
- reverse = not not dict.get('reverse', False),
- march = not not dict.get('march', False)
- )
|