Changeset - f31df46edfdd
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 20 months ago 2023-05-30 02:36:24
drewp@bigasterisk.com
reformat
1 file changed with 12 insertions and 10 deletions:
0 comments (0 inline, 0 general)
light9/effect/effecteval.py
Show inline comments
 
@@ -31,134 +31,136 @@ def literalColor(rnorm, gnorm, bnorm):
 
        int(rnorm * 255),  #
 
        int(gnorm * 255),  #
 
        int(bnorm * 255))))
 

	
 

	
 
def literalColorHsv(h, s, v):
 
    return literalColor(*hsv_to_rgb(h, s, v))
 

	
 

	
 
def nsin(x):
 
    return (math.sin(x * (2 * math.pi)) + 1) / 2
 

	
 

	
 
def ncos(x):
 
    return (math.cos(x * (2 * math.pi)) + 1) / 2
 

	
 

	
 
def nsquare(t, on=.5):
 
    return (t % 1.0) < on
 

	
 

	
 
def lerp(a, b, t):
 
    return a + (b - a) * t
 

	
 

	
 
def noise(t):
 
    return pnoise1(t % 1000.0, 2)
 

	
 

	
 
def clamp(lo, hi, x):
 
    return max(lo, min(hi, x))
 

	
 

	
 
def clamp255(x):
 
    return min(255, max(0, x))
 

	
 

	
 
def _8bit(f):
 
    if not isinstance(f, (int, float)):
 
        raise TypeError(repr(f))
 
    return clamp255(int(f * 255))
 

	
 

	
 
@dataclass
 
class EffectEval2:
 
    graph: SyncedGraph
 
    uri: EffectUri
 

	
 
    effectFunction: Optional[URIRef]=None
 
    effectFunction: Optional[URIRef] = None
 

	
 
    def __post_init__(self):
 
        self.graph.addHandler(self._compile)
 
        self.effectFunction = L9['todo']
 

	
 
    def _compile(self):
 
        if not self.graph.contains((self.uri, RDF.type, L9['Effect'])):
 
            raise ValueError(f'{self.uri} not an :Effect')
 

	
 
        self.function = effect_scale   
 
        self.function = effect_scale
 
        devs = []
 
        for s in self.graph.objects(self.uri, L9['setting']):
 
            d = typedValue(DeviceUri, self.graph, s, L9['device'])     
 
            da = typedValue(DeviceAttr, self.graph, s, L9['deviceAttr'])     
 
            d = typedValue(DeviceUri, self.graph, s, L9['device'])
 
            da = typedValue(DeviceAttr, self.graph, s, L9['deviceAttr'])
 
            v = typedValue(VTUnion, self.graph, s, L9['value'])
 
            devs.append((d, da, v))
 
        self.devs = DeviceSettings(self.graph, devs)
 

	
 
    def compute(self, inputs:EffectSettings) -> DeviceSettings:
 
    def compute(self, inputs: EffectSettings) -> DeviceSettings:
 

	
 
        s=0
 
        for e,ea,v in inputs.asList():
 
        s = 0
 
        for e, ea, v in inputs.asList():
 
            if not isinstance(v, float):
 
                raise TypeError
 
            if ea==L9['strength']:
 
            if ea == L9['strength']:
 
                s = v
 

	
 
        return effect_scale(s,self.devs )
 
        return effect_scale(s, self.devs)
 

	
 
        return self.function(inputs)
 

	
 

	
 
def effect_scale(strength: float, devs: DeviceSettings) -> DeviceSettings:
 
    out = []
 
    for d,da,v in devs.asList():
 
    for d, da, v in devs.asList():
 
        out.append((d, da, scale(v, strength)))
 
    return DeviceSettings(devs.graph, out)
 

	
 

	
 
@dataclass
 
class EffectEval:
 
    """
 
    runs one effect's code to turn effect attr settings into output
 
    device settings. No effect state; suitable for reload().
 
    """
 
    graph: SyncedGraph
 
    effect: EffectClass
 
    simpleOutputs: SimpleOutputs
 

	
 
    def outputFromEffect(self, effectSettings: BareEffectSettings, songTime: float, noteTime: float) -> Tuple[DeviceSettings, Dict]:
 
        """
 
        From effect attr settings, like strength=0.75, to output device
 
        settings like light1/bright=0.72;light2/bright=0.78. This runs
 
        the effect code.
 
        """
 
        # todo: what does the next comment line mean?
 
        # both callers need to apply note overrides
 

	
 
        strength = float(effectSettings.s[EffectAttr(L9['strength'])])
 
        if strength <= 0:
 
            return DeviceSettings(self.graph, []), {'zero': True}
 

	
 
        report = {}
 
        out: Dict[Tuple[DeviceUri, DeviceAttr], VTUnion] = {}
 

	
 
        out.update(self.simpleOutputs.values(self.effect, strength, effectSettings.s.get(EffectAttr(L9['colorScale']), None)))
 

	
 
        if self.effect.startswith(L9['effect/']):
 
            tail = 'effect_' + self.effect[len(L9['effect/']):]
 
            try:
 
                func = globals()[tail]
 
            except KeyError:
 
                report['error'] = 'effect code not found for %s' % self.effect
 
            else:
 
                out.update(func(effectSettings, strength, songTime, noteTime))
 

	
 
        outList = [(d, a, v) for (d, a), v in out.items()]
 
        return DeviceSettings(self.graph, outList), report
 

	
 

	
 
def effect_Curtain(effectSettings, strength, songTime, noteTime):
 
    return {(L9['device/lowPattern%s' % n], L9['color']): literalColor(strength, strength, strength) for n in range(301, 308 + 1)}
 

	
 

	
 
def effect_animRainbow(effectSettings, strength, songTime, noteTime):
 
    out = {}
 
    tint = effectSettings.get(L9['tint'], '#ffffff')
0 comments (0 inline, 0 general)