From c9f39a8b93b75a332acb49db8c9224ecdbe2a863 Mon Sep 17 00:00:00 2001 From: velvettear Date: Wed, 7 Apr 2021 12:04:06 +0200 Subject: [PATCH] fixed some bugs, convert rgb color values to hex --- libs/blinkstick.js | 163 +++++++++++++++++++-------------------------- 1 file changed, 68 insertions(+), 95 deletions(-) diff --git a/libs/blinkstick.js b/libs/blinkstick.js index 964be6d..0ff5d3b 100644 --- a/libs/blinkstick.js +++ b/libs/blinkstick.js @@ -1,14 +1,13 @@ // requirements const logger = require('./logger'); +const config = require('../config.json'); // third party requirements const blinkstick = require('blinkstick'); -const async = require('async'); const hexcolor = require('hex-color-regex'); // constants -const COLORTYPE_RGB = "rgb"; -const COLORTYPE_HEX = "hex"; +const RANDOM = "random" const ANIMATION_STATE_INFINITE = 2; const ANIMATION_STATE_INPROGRESS = 1; const ANIMATION_STATE_FINISH = 0; @@ -34,166 +33,143 @@ function powerOff() { } // parse a color object from given request arguments -function parseColor(blinkstickConfig) { - return new Promise(function (resolve, reject) { - if (blinkstickConfig.color == "random") { - resolve(color); - } - if (blinkstickConfig.color == undefined || blinkstickConfig.color.length === 0) { - reject("no hex color code given, request will be ignored"); - } - let parsedColor = parseRGBColor(blinkstickConfig.color); - if (!parsedColor) { - parsedColor = parseHexColor(blinkstickConfig.color); - } - if (!parsedColor) { - return reject("could not parse given color code '" + color + "'"); - } - blinkstickConfig.color = parsedColor; - resolve(blinkstickConfig); - }); +function parseColor(value) { + if (value == RANDOM) { + return RANDOM; + } + let parsedColor = parseRGBColor(value); + if (!parsedColor) { + parsedColor = parseHexColor(value); + } + return parsedColor || function () { + logger.warn("could not parse color value '" + value + "', defaulting to '" + config.api.post.color.default + "'"); + return config.api.post.color.default; + }(); } // parse rgb color values -function parseRGBColor(colorValues) { - let color = {}; - if (colorValues.indexOf(",") == -1) { - if (isRGBValue(colorValues)) { - color.type = COLORTYPE_RGB; - color.red = parseInt(colorValues); - return color; - } +function parseRGBColor(value) { + if (value.indexOf(",") == -1 && isRGBValue(value)) { + return convertRGBToHex(parseInt(value) || 0, 0, 0); } else { - const splittedValues = colorValues.split(","); + const splittedValues = value.split(","); + let color = {}; for (let index = 0; index < splittedValues.length; index++) { const value = splittedValues[index]; - if (!isRGBValue(value)) { - continue; - } if (index == 0) { - color.red = parseInt(value); + color.red = parseInt(value) || 0; } else if (index === 1) { - color.green = parseInt(value); + color.green = parseInt(value) || 0; } else if (index === 2) { - color.blue = parseInt(value); + color.blue = parseInt(value) || 0; } } - if (color && (color.red || color.green || color.blue)) { - color.type = COLORTYPE_RGB; - return color; + if (isRGBValue(color.red) && isRGBValue(color.green) && isRGBValue(color.blue)) { + return convertRGBToHex(color.red, color.green, color.blue); } } - if (isRGBValue(colorValues.red) || isRGBValue(colorValues.green) || isRGBValue(colorValues.blue)) { - color.type = COLORTYPE_RGB; - return colorValues; - } - return undefined; } // check a single rgb value function isRGBValue(value) { - return value != undefined && value.length > 0 && !isNaN(value) && value >= 0 && value <= 255; + return value != undefined && !isNaN(value) && value >= 0 && value <= 255; +} + +// convert rgb to hex color values +function convertRGBToHex(red, green, blue) { + return "#" + ((1 << 24) + (red << 16) + (green << 8) + blue).toString(16).slice(1); } // parse hex color values -function parseHexColor(colorValues) { - if (colorValues[0] !== '#') { - colorValues = '#' + colorValues; +function parseHexColor(value) { + if (value[0] != '#') { + value = '#' + value; } - if (colorValues.length === 4) { - colorValues.type = COLORTYPE_HEX; - return (colorValues[0] + colorValues[1] + colorValues[1] + colorValues[2] + colorValues[2] + colorValues[3] + colorValues[3]); + if (value.length === 4) { + value = (value[0] + value[1] + value[1] + value[2] + value[2] + value[3] + value[3]); } - if (colorValues.length === 7 && hexcolor({ strict: true }).test(colorValues)) { - colorValues.type = COLORTYPE_HEX; - return colorValues; + if (hexcolor({ strict: true }).test(value)) { + return value; } - return undefined; } // pass the options to the blinkstick accordingly function illuminate(blinkstickConfig) { return new Promise(function (resolve, reject) { - waitForAnimation - let color; - findBlinkstick().then(function () { - if (blinkstickConfig.color.type === COLORTYPE_HEX) { - color = blinkstickConfig.color.red; - } else { - color = blinkstickConfig.color.red + ", " + blinkstickConfig.color.green + ", " + blinkstickConfig.color.blue; - } - switch (blinkstickConfig.mode) { - case "morph": - morph(blinkstickConfig, color, resolve, reject); - break; - case "pulse": - startPulsing(blinkstickConfig, color, resolve, reject); - break; - default: - setColor(blinkstickConfig, color, resolve, reject); - break; - } - }) + findBlinkstick() + .then(function () { + switch (blinkstickConfig.mode) { + case "morph": + morph(blinkstickConfig, resolve, reject); + break; + case "pulse": + startPulsing(blinkstickConfig, resolve, reject); + break; + default: + setColor(blinkstickConfig, resolve, reject); + break; + } + }) .catch(reject); }); } // set a static color -function setColor(blinkstickConfig, color, resolve, reject) { +function setColor(blinkstickConfig, resolve, reject) { stopAnimation() .then(function () { setAnimationProperties(blinkstickConfig); - led.setColor(blinkstickConfig.color.red, blinkstickConfig.color.green, blinkstickConfig.color.blue, function (err) { + logger.debug("setting color to '" + blinkstickConfig.color + "'..."); + led.setColor(blinkstickConfig.color, blinkstickConfig.options, function (err) { clearAnimationProperties(); - logger.debug("setting color to '" + color + "'..."); if (err) { - return reject("error setting color '" + color + "' (" + err + ")"); + return reject("error setting color '" + blinkstickConfig.color + "' (" + err + ")"); } - return resolve("set color to '" + color + "'"); + return resolve("set color to '" + blinkstickConfig.color + "'"); }); }); } // morph to a color -function morph(blinkstickConfig, color, resolve, reject) { +function morph(blinkstickConfig, resolve, reject) { stopAnimation() .then(function () { setAnimationProperties(blinkstickConfig); - led.morph(blinkstickConfig.color.red, blinkstickConfig.color.green, blinkstickConfig.color.blue, blinkstickConfig.options, function (err) { + logger.debug("morphing color to '" + blinkstickConfig.color + "'..."); + led.morph(blinkstickConfig.color, blinkstickConfig.options, function (err) { clearAnimationProperties(); - logger.debug("morphing color to '" + color + "'..."); if (err) { - return reject("error morphing color to '" + color + "' (" + err + ")"); + return reject("error morphing color to '" + blinkstickConfig.color + "' (" + err + ")"); } - return resolve("morphed color to '" + color + "'"); + return resolve("morphed color to '" + blinkstickConfig.color + "'"); }); }); } // start pulsing -function startPulsing(blinkstickConfig, color, resolve, reject) { +function startPulsing(blinkstickConfig, resolve, reject) { if (animation.state == ANIMATION_STATE_FINISH || (blinkstickConfig.options.pulse.max > 0 && (blinkstickConfig.options.pulse.done && blinkstickConfig.options.pulse.done < blinkstickConfig.options.pulse.max))) { clearAnimationProperties(); - return resolve("finished pulsing color '" + color + "'"); + return resolve("finished pulsing color '" + blinkstickConfig.color + "'"); } if (animation.id && animation.id != blinkstickConfig.id) { stopAnimation() .then(logger.info) .then(function () { - startPulsing(blinkstickConfig, color, resolve, reject) + startPulsing(blinkstickConfig, resolve, reject) }) .catch(logger.error); return; } setAnimationProperties(blinkstickConfig); - led.pulse(blinkstickConfig.color.red, blinkstickConfig.color.green, blinkstickConfig.color.blue, blinkstickConfig.options, function (err) { + led.pulse(blinkstickConfig.color, blinkstickConfig.options, function (err) { if (err) { clearAnimationProperties(); - return reject("error pulsing color '" + color + "' (" + err + ")"); + return reject("error pulsing color '" + blinkstickConfig.color + "' (" + err + ")"); } blinkstickConfig.options.pulse.done++; - logger.debug("pulsed color '" + color + "' " + blinkstickConfig.options.pulse.done + "/" + blinkstickConfig.options.pulse.max + " times"); - startPulsing(blinkstickConfig, color, resolve, reject); + logger.debug("pulsed color '" + blinkstickConfig.color + "' " + blinkstickConfig.options.pulse.done + "/" + blinkstickConfig.options.pulse.max + " times"); + startPulsing(blinkstickConfig, resolve, reject); }); } @@ -252,8 +228,5 @@ function isInfiniteAnimationInProgress() { module.exports = { parseColor, illuminate, - isAnimationInProgress, - isInfiniteAnimationInProgress, - stopAnimation, powerOff }; \ No newline at end of file