From 421c3be3dd88a925d5a569fa42459c0aad1a3338 Mon Sep 17 00:00:00 2001 From: velvettear Date: Fri, 25 Feb 2022 15:39:32 +0100 Subject: [PATCH] optimized color parsing --- libs/parser.js | 55 ++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/libs/parser.js b/libs/parser.js index 179d9d3..e5a1235 100644 --- a/libs/parser.js +++ b/libs/parser.js @@ -1,7 +1,6 @@ -const logger = require('./logger.js'); const hexcolor = require('hex-color-regex'); -const LEDS_ALL = require('./blinkstick.js').LEDS_ALL; +const LEDS_ALL = require('./blinkstick.js').ALL; const MODE_SET = require('./blinkstick.js').MODE_SET; const MODE_MORPH = require('./blinkstick.js').MODE_MORPH; const MODE_BLINK = require('./blinkstick.js').MODE_BLINK; @@ -115,36 +114,33 @@ function parseColor(value) { if (value === undefined || value === COLOR_RANDOM) { return COLOR_RANDOM; } - let parsedColor = parseRGBColor(value); - if (!parsedColor) { - parsedColor = parseHexColor(value); + let parsedColor = parseHexColor(value); + if (parsedColor !== undefined) { + return parsedColor; } - return parsedColor || function () { - logger.warn('could not parse color value \'' + value + '\', defaulting to \'' + COLOR_RANDOM + '\''); - return COLOR_RANDOM; - }(); + return parseRGBColor(value) || COLOR_RANDOM; } function parseRGBColor(value) { if (value.indexOf(',') === -1 && isRGBValue(value)) { - return convertRGBToHex(parseInt(value) || 0, 0, 0); - } else { - const splittedValues = value.split(','); - let color = {}; - for (let index = 0; index < splittedValues.length; index++) { - const value = splittedValues[index]; - if (index === 0) { - color.red = parseInt(value) || 0; - } else if (index === 1) { - color.green = parseInt(value) || 0; - } else if (index === 2) { - color.blue = parseInt(value) || 0; - } - } - if (isRGBValue(color.red) && isRGBValue(color.green) && isRGBValue(color.blue)) { - return convertRGBToHex(color.red, color.green, color.blue); + value = parseInt(value); + return convertRGBToHex(value, value, value); + } + const splittedValues = value.split(','); + let color = {}; + for (let index = 0; index < splittedValues.length; index++) { + const value = splittedValues[index]; + if (index === 0) { + color.red = parseInt(value) || 0; + } else if (index === 1) { + color.green = parseInt(value) || 0; + } else if (index === 2) { + color.blue = parseInt(value) || 0; } } + if (isRGBValue(color.red) && isRGBValue(color.green) && isRGBValue(color.blue)) { + return convertRGBToHex(color.red, color.green, color.blue); + } } function isRGBValue(value) { @@ -159,12 +155,13 @@ function parseHexColor(value) { if (value[0] !== '#') { value = '#' + value; } - if (value.length === 4) { - value = (value[0] + value[1] + value[1] + value[2] + value[2] + value[3] + value[3]); + if (value.length !== 7) { + return; } - if (hexcolor({strict: true}).test(value)) { - return value; + if (!hexcolor({strict: true}).test(value)) { + return; } + return value; } // exports