From 6ec2d66fb0b676338c312a14317c95db49a0cfba Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Fri, 22 Dec 2017 16:07:38 +0100 Subject: [PATCH] Changed color definition to use hex strings --- fs/colors.cfg | 32 ++++++++++++++--------------- fs/init.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 19 deletions(-) diff --git a/fs/colors.cfg b/fs/colors.cfg index 7ade66f..0196e87 100644 --- a/fs/colors.cfg +++ b/fs/colors.cfg @@ -1,18 +1,18 @@ [ - {"name": "off", "red": 0, "green": 0, "blue": 0}, - {"name": "candle", "red": 30, "green": 15, "blue": 4}, - {"name": "25w", "red": 35, "green": 15, "blue": 6}, - {"name": "40w", "red": 50, "green": 25, "blue": 10}, - {"name": "60w", "red": 70, "green": 45, "blue": 20}, - {"name": "75w", "red": 80, "green": 70, "blue": 30}, - {"name": "100w", "red": 100, "green": 85, "blue": 50}, - {"name": "neon", "red": 100, "green": 95, "blue": 70}, - {"name": "red", "red": 50, "green": 5, "blue": 5}, - {"name": "green", "red": 5, "green": 50, "blue": 5}, - {"name": "blue", "red": 5, "green": 5, "blue": 80}, - {"name": "yellow", "red": 50, "green": 50, "blue": 5}, - {"name": "violet", "red": 50, "green": 5, "blue": 80}, - {"name": "warmWhite", "red": 90, "green": 90, "blue": 30}, - {"name": "coldWhite", "red": 80, "green": 80, "blue": 100}, - {"name": "brightWhite", "red": 255, "green": 255, "blue": 255} + {"name": "off", "color": "#000"}, + {"name": "candle", "color": "#201004"}, + {"name": "25w", "color": "#231006"}, + {"name": "40w", "color": "#32190e"}, + {"name": "60w", "color": "#463014"}, + {"name": "75w", "color": "#504620"}, + {"name": "100w", "color": "#645542"}, + {"name": "neon", "color": "#646046"}, + {"name": "red", "color": "#320505"}, + {"name": "green", "color": "#053205"}, + {"name": "blue", "color": "#050550"}, + {"name": "yellow", "color": "#323205"}, + {"name": "violet", "color": "#320550"}, + {"name": "warmWhite", "color": "#5a5a1e"}, + {"name": "coldWhite", "color": "#505064"}, + {"name": "brightWhite", "color": "#ffffff"} ] diff --git a/fs/init.js b/fs/init.js index 0991a38..83f5e7f 100644 --- a/fs/init.js +++ b/fs/init.js @@ -97,6 +97,54 @@ function allLedOff() { NeoPixel_show(); } +function validateColor(color) { + if (color.name.length === 0) { + print('ERROR: Color name missing!'); + return false; + } + if ((color.color.length !== 4 && color.color.length !== 7) || color.color[0] !== '#') { + print('ERROR: Color', color.name, 'has an invalid color definition.'); + print(' Is:', color.color, ' but should #rgb where r/g/b is a 1-digit hex number in the range of 0..9a..f or #RGB where R/G/B is a 2-digit hex number'); + return false; + } + for (let i=0; i '9' && (color.color[i] < 'a' || color.color[i] > 'f'))) { + print('ERROR: Color', color.name, 'has an invalid color definition.'); + print(' Is:', color.color, ' but should #rgb where r/g/b is a 1-digit hex number in the range of 0..9a..f or #RGB where R/G/B is a 2-digit hex number'); + return false; + } + } + return true; +} + +function hexToDec(digit) { + if (digit >= 'a') return digit - 'a'; + else if (digit >= 'A') return digit - 'A'; + else return digit - '0'; +} + +function getRedOfColorString(color) { + if (color.length === 4) { + return hexToDec(color.color[1]); + } else { + return hexToDec(color.color[1]) * 16 + hexToDec(color.color[2]); + } +} +function getGreenOfColorString(color) { + if (color.length === 4) { + return hexToDec(color.color[2]); + } else { + return hexToDec(color.color[3]) * 16 + hexToDec(color.color[4]); + } +} +function getBlueOfColorString(color) { + if (color.length === 4) { + return hexToDec(color.color[3]); + } else { + return hexToDec(color.color[5]) * 16 + hexToDec(color.color[6]); + } +} + function loadColorDefs() { // Load Color definitions let json = File.read(colorFile); @@ -109,7 +157,10 @@ function loadColorDefs() { colors = JSON.parse(json); } for (i=0; i