From d0bad861ae9f344a18b9d0c2cb851a505ccb954f Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Thu, 16 Dec 2021 16:09:10 +0100 Subject: [PATCH] HLS-based color correction --- wibe.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/wibe.py b/wibe.py index f7035fe..a932774 100644 --- a/wibe.py +++ b/wibe.py @@ -7,6 +7,7 @@ import json import time import math from pprint import pprint +import colorsys wledPalettes = {'colorsOnly': 5, 'colorGradient': 4, '2Colors': 3} @@ -31,7 +32,7 @@ def main(token, ip='192.168.3.42', bri=51): #pprint(palette) speed = min(int((feat['tempo']/30*feat['energy'])**2.0)*10,255) power = min(8 + int(((feat['energy']*10)**2.0)*0.5),255) - pprint(requests.post(url, json={"seg": [{"col": palette, "pal": wledPalettes['colorsOnly'], 'sx': speed, 'ix': power}]}).json()) + pprint(requests.post(url, json={"seg": [{"col": palette, "pal": wledPalettes['colorGradient'], 'sx': speed, 'ix': power}]}).json()) time.sleep(3) time.sleep(1) @@ -39,14 +40,25 @@ def getPalette(curTrack,numColors): imgStream = requests.get(curTrack["album"]["images"][-1]["url"]).content #Get the lowest res cover imgFile = io.BytesIO(imgStream) print("[i] Generating palette") - palette = [normalizeColor([col.rgb[c] for c in range(3)]+[0]) for col in sorted(colorgram.extract(imgFile, numColors), key=lambda c: c.proportion + 0*c.hsl.h)] + palette = [normalizeColor([col.rgb[c] for c in range(3)]+[0]) for col in sorted(colorgram.extract(imgFile, numColors), key=lambda c: c.proportion*0 + c.hsl.h)] return palette -def normalizeColor(rgbw): +def normalizeColorByLength(rgbw): s = max(max(rgbw),1) fac = (1 + 255/s)/2 return [min(int(c*fac),255) for c in rgbw] +def normalizeColorByHue(rgbw, strength=1.0): + x = strength + y = 1-x + r, g, b, w = rgbw + h, l, s = colorsys.rgb_to_hls(r/255, g/255, b/255) + r, g, b = colorsys.hls_to_rgb(h, 0.5*x+l*y, 1*x+s*y) + return [r*255, g*255, b*255] + +def normalizeColor(rgbw): + return normalizeColorByHue(rgbw, strength=0.5) + def genSpoToken(username, client_id, client_secret): return spoUtil.prompt_for_user_token(username,"user-read-currently-playing",client_id=client_id,client_secret=client_secret,redirect_uri='http://localhost/')