From 3c59690f30e343b8286a55932e81c25ff2c2d417 Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Mon, 13 Apr 2020 16:28:38 +0300 Subject: [PATCH] feat: multibit support for rgb lights --- src/modules/saving/templates/light.ts | 2 ++ src/modules/saving/templates/rgb.ts | 13 +++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/modules/saving/templates/light.ts b/src/modules/saving/templates/light.ts index 96a1fd1..ab273f9 100644 --- a/src/modules/saving/templates/light.ts +++ b/src/modules/saving/templates/light.ts @@ -24,6 +24,8 @@ const lightTemplate: PartialTemplate = { activation: ` const { main, active } = context.colors + const bits = context.get(0) + context.color(parseInt(context.get(0),2) ? active : main) ` }, diff --git a/src/modules/saving/templates/rgb.ts b/src/modules/saving/templates/rgb.ts index a55c4a6..b4e0069 100644 --- a/src/modules/saving/templates/rgb.ts +++ b/src/modules/saving/templates/rgb.ts @@ -22,15 +22,20 @@ const rgbLightTemplate: PartialTemplate = { }, code: { activation: ` - const get = (index) => context.getBinary(index) & 1 - const color = (get(0) << 2) + (get(1) << 1) + get(2) + const get = (index) => context.get(index) + + const colors = Array.from({ length: 3 }, (_, index) => { + const bits = get(index) + const max = 2 ** bits.length - 1 + return 256 * parseInt(bits, 2) / max + }) - if (color === 0){ + if (colors.reduce((curr, acc) => acc + curr, 0) === 0){ context.color(context.colors.main) } else { - context.color(context.colors[color]) + context.color(\`rgb(\${colors.join(",")})\`) } ` },