fixed some bugs, convert rgb color values to hex

This commit is contained in:
Daniel Sommer 2021-04-07 12:04:06 +02:00
parent 4dca1d43b0
commit c9f39a8b93

View file

@ -1,14 +1,13 @@
// requirements // requirements
const logger = require('./logger'); const logger = require('./logger');
const config = require('../config.json');
// third party requirements // third party requirements
const blinkstick = require('blinkstick'); const blinkstick = require('blinkstick');
const async = require('async');
const hexcolor = require('hex-color-regex'); const hexcolor = require('hex-color-regex');
// constants // constants
const COLORTYPE_RGB = "rgb"; const RANDOM = "random"
const COLORTYPE_HEX = "hex";
const ANIMATION_STATE_INFINITE = 2; const ANIMATION_STATE_INFINITE = 2;
const ANIMATION_STATE_INPROGRESS = 1; const ANIMATION_STATE_INPROGRESS = 1;
const ANIMATION_STATE_FINISH = 0; const ANIMATION_STATE_FINISH = 0;
@ -34,103 +33,80 @@ function powerOff() {
} }
// parse a color object from given request arguments // parse a color object from given request arguments
function parseColor(blinkstickConfig) { function parseColor(value) {
return new Promise(function (resolve, reject) { if (value == RANDOM) {
if (blinkstickConfig.color == "random") { return RANDOM;
resolve(color);
} }
if (blinkstickConfig.color == undefined || blinkstickConfig.color.length === 0) { let parsedColor = parseRGBColor(value);
reject("no hex color code given, request will be ignored");
}
let parsedColor = parseRGBColor(blinkstickConfig.color);
if (!parsedColor) { if (!parsedColor) {
parsedColor = parseHexColor(blinkstickConfig.color); parsedColor = parseHexColor(value);
} }
if (!parsedColor) { return parsedColor || function () {
return reject("could not parse given color code '" + color + "'"); logger.warn("could not parse color value '" + value + "', defaulting to '" + config.api.post.color.default + "'");
} return config.api.post.color.default;
blinkstickConfig.color = parsedColor; }();
resolve(blinkstickConfig);
});
} }
// parse rgb color values // parse rgb color values
function parseRGBColor(colorValues) { function parseRGBColor(value) {
let color = {}; if (value.indexOf(",") == -1 && isRGBValue(value)) {
if (colorValues.indexOf(",") == -1) { return convertRGBToHex(parseInt(value) || 0, 0, 0);
if (isRGBValue(colorValues)) {
color.type = COLORTYPE_RGB;
color.red = parseInt(colorValues);
return color;
}
} else { } else {
const splittedValues = colorValues.split(","); const splittedValues = value.split(",");
let color = {};
for (let index = 0; index < splittedValues.length; index++) { for (let index = 0; index < splittedValues.length; index++) {
const value = splittedValues[index]; const value = splittedValues[index];
if (!isRGBValue(value)) {
continue;
}
if (index == 0) { if (index == 0) {
color.red = parseInt(value); color.red = parseInt(value) || 0;
} else if (index === 1) { } else if (index === 1) {
color.green = parseInt(value); color.green = parseInt(value) || 0;
} else if (index === 2) { } else if (index === 2) {
color.blue = parseInt(value); color.blue = parseInt(value) || 0;
} }
} }
if (color && (color.red || color.green || color.blue)) { if (isRGBValue(color.red) && isRGBValue(color.green) && isRGBValue(color.blue)) {
color.type = COLORTYPE_RGB; return convertRGBToHex(color.red, color.green, color.blue);
return color;
} }
} }
if (isRGBValue(colorValues.red) || isRGBValue(colorValues.green) || isRGBValue(colorValues.blue)) {
color.type = COLORTYPE_RGB;
return colorValues;
}
return undefined;
} }
// check a single rgb value // check a single rgb value
function isRGBValue(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 // parse hex color values
function parseHexColor(colorValues) { function parseHexColor(value) {
if (colorValues[0] !== '#') { if (value[0] != '#') {
colorValues = '#' + colorValues; value = '#' + value;
} }
if (colorValues.length === 4) { if (value.length === 4) {
colorValues.type = COLORTYPE_HEX; value = (value[0] + value[1] + value[1] + value[2] + value[2] + value[3] + value[3]);
return (colorValues[0] + colorValues[1] + colorValues[1] + colorValues[2] + colorValues[2] + colorValues[3] + colorValues[3]);
} }
if (colorValues.length === 7 && hexcolor({ strict: true }).test(colorValues)) { if (hexcolor({ strict: true }).test(value)) {
colorValues.type = COLORTYPE_HEX; return value;
return colorValues;
} }
return undefined;
} }
// pass the options to the blinkstick accordingly // pass the options to the blinkstick accordingly
function illuminate(blinkstickConfig) { function illuminate(blinkstickConfig) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
waitForAnimation findBlinkstick()
let color; .then(function () {
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) { switch (blinkstickConfig.mode) {
case "morph": case "morph":
morph(blinkstickConfig, color, resolve, reject); morph(blinkstickConfig, resolve, reject);
break; break;
case "pulse": case "pulse":
startPulsing(blinkstickConfig, color, resolve, reject); startPulsing(blinkstickConfig, resolve, reject);
break; break;
default: default:
setColor(blinkstickConfig, color, resolve, reject); setColor(blinkstickConfig, resolve, reject);
break; break;
} }
}) })
@ -139,61 +115,61 @@ function illuminate(blinkstickConfig) {
} }
// set a static color // set a static color
function setColor(blinkstickConfig, color, resolve, reject) { function setColor(blinkstickConfig, resolve, reject) {
stopAnimation() stopAnimation()
.then(function () { .then(function () {
setAnimationProperties(blinkstickConfig); 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(); clearAnimationProperties();
logger.debug("setting color to '" + color + "'...");
if (err) { 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 // morph to a color
function morph(blinkstickConfig, color, resolve, reject) { function morph(blinkstickConfig, resolve, reject) {
stopAnimation() stopAnimation()
.then(function () { .then(function () {
setAnimationProperties(blinkstickConfig); 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(); clearAnimationProperties();
logger.debug("morphing color to '" + color + "'...");
if (err) { 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 // 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))) { if (animation.state == ANIMATION_STATE_FINISH || (blinkstickConfig.options.pulse.max > 0 && (blinkstickConfig.options.pulse.done && blinkstickConfig.options.pulse.done < blinkstickConfig.options.pulse.max))) {
clearAnimationProperties(); clearAnimationProperties();
return resolve("finished pulsing color '" + color + "'"); return resolve("finished pulsing color '" + blinkstickConfig.color + "'");
} }
if (animation.id && animation.id != blinkstickConfig.id) { if (animation.id && animation.id != blinkstickConfig.id) {
stopAnimation() stopAnimation()
.then(logger.info) .then(logger.info)
.then(function () { .then(function () {
startPulsing(blinkstickConfig, color, resolve, reject) startPulsing(blinkstickConfig, resolve, reject)
}) })
.catch(logger.error); .catch(logger.error);
return; return;
} }
setAnimationProperties(blinkstickConfig); 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) { if (err) {
clearAnimationProperties(); clearAnimationProperties();
return reject("error pulsing color '" + color + "' (" + err + ")"); return reject("error pulsing color '" + blinkstickConfig.color + "' (" + err + ")");
} }
blinkstickConfig.options.pulse.done++; blinkstickConfig.options.pulse.done++;
logger.debug("pulsed color '" + color + "' " + blinkstickConfig.options.pulse.done + "/" + blinkstickConfig.options.pulse.max + " times"); logger.debug("pulsed color '" + blinkstickConfig.color + "' " + blinkstickConfig.options.pulse.done + "/" + blinkstickConfig.options.pulse.max + " times");
startPulsing(blinkstickConfig, color, resolve, reject); startPulsing(blinkstickConfig, resolve, reject);
}); });
} }
@ -252,8 +228,5 @@ function isInfiniteAnimationInProgress() {
module.exports = { module.exports = {
parseColor, parseColor,
illuminate, illuminate,
isAnimationInProgress,
isInfiniteAnimationInProgress,
stopAnimation,
powerOff powerOff
}; };