Changeset - 97a7c71b952a
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 6 years ago 2019-06-10 01:39:26
drewp@bigasterisk.com
1 file changed with 52 insertions and 46 deletions:
0 comments (0 inline, 0 general)
light9/effect/effecteval.py
Show inline comments
 
@@ -2,25 +2,24 @@ from rdflib import Literal, URIRef, Name
 
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),
 
@@ -55,24 +54,25 @@ 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):
 
@@ -394,140 +394,146 @@ def effect_lightning(effectSettings, str
 
    ]
 
    out = {}
 
    col = rgb_to_hex([int(255 * strength)] * 3)
 
    for i, dev in enumerate(devs):
 
        n = noise(songTime * 8 + i * 6.543)
 
        if n > .4:
 
            out[(dev, L9['color'])] = col
 
    return out
 

	
 

	
 
def sample8(img, x, y, repeat=False):
 
    if not (0 <= y < img.height):
 
        return (0,0,0)
 
        return (0, 0, 0)
 
    if 0 <= x < img.width:
 
        return img.getpixel((x, y))
 
    elif not repeat:
 
        return (0, 0, 0)
 
    else:
 
        return img.getpixel((x % img.width, y))
 

	
 

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

	
 
    colorScale = hex_to_rgb(effectSettings.get(L9['colorScale'], '#ffffff'))
 

	
 
    for dev, y in [(SKY['strip1'], 0),
 
                   (SKY['strip2'], 1),
 
                   (SKY['strip3'], 2),
 
                   (SKY['par3'], 3), # dl
 
                   (SKY['par4'], 4), # ul
 
                   (SKY['par7'], 5), # ur
 
                   (SKY['par1'], 6), # dr
 
                   ('cyc1', 7),
 
                   ('cyc2', 8),
 
                   ('cyc3', 9),
 
                   ('cyc4', 10),
 
                   ('down1', 11),
 
                   ('down2', 12),
 
                   ('down3', 13),
 
                   ('down4', 14),
 
                   ('down5', 15),
 
                   ('down6', 16),
 
                   ('down7', 17),
 
    for dev, y in [
 
        (SKY['strip1'], 0),
 
        (SKY['strip2'], 1),
 
        (SKY['strip3'], 2),
 
        (SKY['par3'], 3),  # dl
 
        (SKY['par4'], 4),  # ul
 
        (SKY['par7'], 5),  # ur
 
        (SKY['par1'], 6),  # dr
 
        ('cyc1', 7),
 
        ('cyc2', 8),
 
        ('cyc3', 9),
 
        ('cyc4', 10),
 
        ('down1', 11),
 
        ('down2', 12),
 
        ('down3', 13),
 
        ('down4', 14),
 
        ('down5', 15),
 
        ('down6', 16),
 
        ('down7', 17),
 
    ]:
 
        color8 = sample8(img, x, y, effectSettings.get(L9['repeat'], True))
 
        color = map(lambda v: v / 255 * strength, color8)
 
        color = [v * cs / 255 for v, cs in zip(color, colorScale)]
 
        if dev in ['cyc1', 'cyc2', 'cyc3', 'cyc4']:
 
            column = dev[-1]
 
            out[(SKY[f'cycRed{column}'],   L9['brightness'])] = color[0]
 
            out[(SKY[f'cycRed{column}'], L9['brightness'])] = color[0]
 
            out[(SKY[f'cycGreen{column}'], L9['brightness'])] = color[1]
 
            out[(SKY[f'cycBlue{column}'],  L9['brightness'])] = color[2]
 
            out[(SKY[f'cycBlue{column}'], L9['brightness'])] = color[2]
 
        else:
 
            out[(dev, L9['color'])] = rgb_to_hex(map(_8bit, color))
 
    return out
 

	
 

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

	
 
    out ={
 
    out = {
 
        (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'],
 
    ]
 
    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)
 
        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
0 comments (0 inline, 0 general)