Changeset - 84a9527508c6
[Not reviewed]
default
0 2 0
drewp@bigasterisk.com - 6 years ago 2019-06-09 23:16:04
drewp@bigasterisk.com
some new effects
Ignore-this: 16a5592769171a2426c0e317b1e0401a
2 files changed with 86 insertions and 16 deletions:
0 comments (0 inline, 0 general)
light9/effect/effecteval.py
Show inline comments
 
from rdflib import Literal, URIRef
 
from rdflib import Literal, URIRef, Namespace
 
from light9.namespaces import L9, DEV
 
from webcolors import rgb_to_hex, hex_to_rgb
 
from colorsys import hsv_to_rgb
 
import math
 
from noise import pnoise1
 
import logging
 
from light9.effect.settings import DeviceSettings
 
from light9.effect.scale import scale
 
from typing import Dict, Tuple, Any
 
from PIL import Image
 
import random
 

	
 

	
 
SKY = Namespace('http://light9.bigasterisk.com/theater/skyline/device/')
 

	
 
random.seed(0)
 

	
 
log = logging.getLogger('effecteval')
 
log.info("reload effecteval")
 

	
 

	
 
def literalColor(rnorm, gnorm, bnorm):
 
    return Literal(
 
        rgb_to_hex([int(rnorm * 255),
 
                    int(gnorm * 255),
 
                    int(bnorm * 255)]))
 

	
 
@@ -42,24 +46,33 @@ def nsquare(t, on=.5):
 
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))
 

	
 
class EffectEval(object):
 
    """
 
    runs one effect's code to turn effect attr settings into output
 
    device settings. No state; suitable for reload().
 
    """
 

	
 
    def __init__(self, graph, effect, simpleOutputs):
 
        self.graph = graph
 
        self.effect = effect
 
        self.simpleOutputs = simpleOutputs
 

	
 
    def outputFromEffect(self, effectSettings, songTime, noteTime):
 
@@ -397,40 +410,95 @@ def sample(img, x, y, repeat=False):
 
        return img.getpixel((x % img.width, y))
 

	
 

	
 
def effect_image(effectSettings, strength, songTime, noteTime):
 
    out = {}
 
    imgPath = f'cur/anim/{effectSettings[L9["image"]]}'
 
    t_offset = effectSettings.get(L9['tOffset'], 0)
 
    pxPerSec = effectSettings.get(L9['pxPerSec'], 30)
 
    img = Image.open(imgPath)
 
    x = (noteTime * pxPerSec)
 

	
 
    scl = effectSettings.get(L9['strength'], 1)
 
    for dev, y in [(L9['theater/skyline/device/strip1'], 0),
 
                   (L9['theater/skyline/device/strip2'], 1),
 
                   (L9['theater/skyline/device/strip3'], 2)]:
 
    for dev, y in [(SKY['strip1'], 0),
 
                   (SKY['strip2'], 1),
 
                   (SKY['strip3'], 2)]:
 
        color = sample(img, x, y, effectSettings.get(L9['repeat'], False))
 
        out[(dev, L9['color'])] = scale(rgb_to_hex(color), scl)
 
    return out
 

	
 
def effect_cyc(effectSettings, strength, songTime, noteTime):
 
    colorScale = effectSettings.get(L9['colorScale'], '#ffffff')
 
    r, g, b = map(lambda x: x / 255, hex_to_rgb(colorScale))
 

	
 
    out ={
 
        (L9['theater/skyline/device/cycRed1'], L9['brightness']): r,
 
        (L9['theater/skyline/device/cycRed2'], L9['brightness']): r,
 
        (L9['theater/skyline/device/cycRed3'], L9['brightness']): r,
 
        (L9['theater/skyline/device/cycRed4'], L9['brightness']): r,
 
        (L9['theater/skyline/device/cycGreen1'], L9['brightness']): g,
 
        (L9['theater/skyline/device/cycGreen2'], L9['brightness']): g,
 
        (L9['theater/skyline/device/cycGreen3'], L9['brightness']): g,
 
        (L9['theater/skyline/device/cycGreen4'], L9['brightness']): g,
 
        (L9['theater/skyline/device/cycBlue1'], L9['brightness']): b,
 
        (L9['theater/skyline/device/cycBlue2'], L9['brightness']): b,
 
        (L9['theater/skyline/device/cycBlue3'], L9['brightness']): b,
 
        (L9['theater/skyline/device/cycBlue4'], L9['brightness']): b,
 
        (SKY['cycRed1'], L9['brightness']): r,
 
        (SKY['cycRed2'], L9['brightness']): r,
 
        (SKY['cycRed3'], L9['brightness']): r,
 
        (SKY['cycRed4'], L9['brightness']): r,
 
        (SKY['cycGreen1'], L9['brightness']): g,
 
        (SKY['cycGreen2'], L9['brightness']): g,
 
        (SKY['cycGreen3'], L9['brightness']): g,
 
        (SKY['cycGreen4'], L9['brightness']): g,
 
        (SKY['cycBlue1'], L9['brightness']): b,
 
        (SKY['cycBlue2'], L9['brightness']): b,
 
        (SKY['cycBlue3'], L9['brightness']): b,
 
        (SKY['cycBlue4'], L9['brightness']): b,
 
         
 
        }
 

	
 
    return out
 

	
 
cycChase1_members = [
 
       SKY['cycRed1'], 
 
       SKY['cycRed2'], 
 
       SKY['cycRed3'], 
 
       SKY['cycRed4'], 
 
       SKY['cycGreen1'], 
 
       SKY['cycGreen2'], 
 
       SKY['cycGreen3'], 
 
       SKY['cycGreen4'], 
 
       SKY['cycBlue1'], 
 
       SKY['cycBlue2'], 
 
       SKY['cycBlue3'], 
 
       SKY['cycBlue4'],
 
    ]
 
cycChase1_members = cycChase1_members * 20
 
random.shuffle(cycChase1_members)
 

	
 
def effect_cycChase1(effectSettings, strength, songTime, noteTime):
 
    colorScale = effectSettings.get(L9['colorScale'], '#ffffff')
 
    r, g, b = map(lambda x: x / 255, hex_to_rgb(colorScale))
 
    tintAmount = {'Red': r, 'Green': g, 'Blue': b}
 

	
 
    members = cycChase1_members
 

	
 
    out = {}
 
    period = float(effectSettings.get(L9['period'], 6 / len(members)))
 

	
 
    for i, dev in enumerate(members):
 
        cursor = (songTime / period) % float(len(members))
 
        dist = abs(i - cursor)
 
        radius = 7
 
        if dist < radius:
 
            colorFromUri = str(dev).split('/')[-1].split('cyc')[1][:-1]
 
            scale = strength * tintAmount[colorFromUri]
 
            out.update({
 
                (dev, L9['brightness']): (1 - dist / radius) * scale,
 
            })
 
    return out
 

	
 

	
 
def effect_parNoise(effectSettings, strength, songTime, noteTime):
 
    
 
    colorScale = effectSettings.get(L9['colorScale'], '#ffffff')
 
    r, g, b = map(lambda x: x / 255, hex_to_rgb(colorScale))
 
    out = {}
 
    speed = 10
 
    gamma = .6
 
    for dev in [SKY['strip1'], SKY['strip2'], SKY['strip3']]:
 
        out[(dev, L9['color'])] = scale(rgb_to_hex(
 
            (_8bit(r * math.pow(max(.01, noise(speed * songTime)), gamma)),
 
             _8bit(g * math.pow(max(.01, noise(speed * songTime + 10)), gamma)),
 
             _8bit(b * math.pow(max(.01, noise(speed * songTime + 20)), gamma)))), strength)
 

	
 
    return out
show/dance2019/effects.n3
Show inline comments
 
@prefix : <http://light9.bigasterisk.com/> .
 
@prefix e: <http://light9.bigasterisk.com/effect/> .
 
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
 
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
 

	
 
e:cyc a :Effect ; rdfs:label "cyc".
 
e:cycChase1 a :Effect ; rdfs:label "cycChase1".
 
e:parNoise a :Effect ; rdfs:label "parNoise".
 
\ No newline at end of file
0 comments (0 inline, 0 general)