From 96da040eafdcb169494944499390cc4baedb4fb5 Mon Sep 17 00:00:00 2001 From: Leland Sindt Date: Fri, 17 Sep 2021 21:36:33 -0500 Subject: [PATCH] scale and clamp brightness --- lumen.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lumen.py b/lumen.py index 3dc04e4..df1dc1b 100644 --- a/lumen.py +++ b/lumen.py @@ -250,7 +250,7 @@ def pixelWrapper(pixel, color): def parseMaxBright(payload): try: - return json.loads(payload).get('max_bright',255) + return clamp(scale(json.loads(payload).get('max_bright',255), 0, 100, 0, 255), 0, 255) except: return 255 @@ -267,7 +267,7 @@ def parseCommand(payload): command['length'] = percent if command['length'] > 100: command['length'] = 100 - command['bright'] = command.get('bright', 255) + command['bright'] = clamp(scale(command.get('bright', 100), 0, 100, 0, 255), 0, 255) command['velocity'] = command.get('velocity', 100) command['r'] = int(command.get('r', 0)) command['g'] = int(command.get('g', 0)) @@ -291,6 +291,12 @@ def parseCommand(payload): command['w2'] = int(rgbw2.split(',')[3]) return command +def scale(x, in_min, in_max, out_min, out_max): + return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) + +def clamp(num, min_value, max_value): + return max(min(num, max_value), min_value) + lumenCommand = parseCommand('{}') myServer = HTTPServer(('', hostPort), MyServer)