Changed color definition to use hex strings

This commit is contained in:
Dirk Jahnke 2017-12-22 16:07:38 +01:00
parent 4aeebe1b33
commit 6ec2d66fb0
2 changed files with 70 additions and 19 deletions

View File

@ -1,18 +1,18 @@
[ [
{"name": "off", "red": 0, "green": 0, "blue": 0}, {"name": "off", "color": "#000"},
{"name": "candle", "red": 30, "green": 15, "blue": 4}, {"name": "candle", "color": "#201004"},
{"name": "25w", "red": 35, "green": 15, "blue": 6}, {"name": "25w", "color": "#231006"},
{"name": "40w", "red": 50, "green": 25, "blue": 10}, {"name": "40w", "color": "#32190e"},
{"name": "60w", "red": 70, "green": 45, "blue": 20}, {"name": "60w", "color": "#463014"},
{"name": "75w", "red": 80, "green": 70, "blue": 30}, {"name": "75w", "color": "#504620"},
{"name": "100w", "red": 100, "green": 85, "blue": 50}, {"name": "100w", "color": "#645542"},
{"name": "neon", "red": 100, "green": 95, "blue": 70}, {"name": "neon", "color": "#646046"},
{"name": "red", "red": 50, "green": 5, "blue": 5}, {"name": "red", "color": "#320505"},
{"name": "green", "red": 5, "green": 50, "blue": 5}, {"name": "green", "color": "#053205"},
{"name": "blue", "red": 5, "green": 5, "blue": 80}, {"name": "blue", "color": "#050550"},
{"name": "yellow", "red": 50, "green": 50, "blue": 5}, {"name": "yellow", "color": "#323205"},
{"name": "violet", "red": 50, "green": 5, "blue": 80}, {"name": "violet", "color": "#320550"},
{"name": "warmWhite", "red": 90, "green": 90, "blue": 30}, {"name": "warmWhite", "color": "#5a5a1e"},
{"name": "coldWhite", "red": 80, "green": 80, "blue": 100}, {"name": "coldWhite", "color": "#505064"},
{"name": "brightWhite", "red": 255, "green": 255, "blue": 255} {"name": "brightWhite", "color": "#ffffff"}
] ]

View File

@ -97,6 +97,54 @@ function allLedOff() {
NeoPixel_show(); 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<color.color.length; ++i) {
if (color.color[i] < '0' || (color.color[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() { function loadColorDefs() {
// Load Color definitions // Load Color definitions
let json = File.read(colorFile); let json = File.read(colorFile);
@ -109,7 +157,10 @@ function loadColorDefs() {
colors = JSON.parse(json); colors = JSON.parse(json);
} }
for (i=0; i<colors.length; ++i) { for (i=0; i<colors.length; ++i) {
print('- addColor', colors[i].name, colors[i].red, colors[i].green, colors[i].blue); print('- addColor', colors[i].name, colors[i].color);
colors[i].red = getRedOfColorString(colors[i]);
colors[i].green = getGreenOfColorString(colors[i]);
colors[i].blue = getBlueOfColorString(colors[i]);
addColor(colors[i].name, colors[i].red, colors[i].green, colors[i].blue); addColor(colors[i].name, colors[i].red, colors[i].green, colors[i].blue);
} }
json = null; json = null;
@ -327,11 +378,11 @@ function recoverFile(filename) {
} }
print(' led.putColors'); print(' led.putColors');
RPC.addHandler('led.putColors', function(args) { RPC.addHandler('led.putColors', function(args) {
if (args !== undefined && args.colors !== undefined) { if (args !== undefined && args.colors !== undefined && args.colors[0] !== undefined && args.colors[0].name !== undefined && args.colors[0].color !== undefined) {
saveFile(colorFile, args.colors); saveFile(colorFile, args.colors);
return { result: 'ok' }; return { result: 'ok' };
} else { } else {
return { error: 'colors: {name, red, green, blue} is required' }; return { error: 'colors: {name, color} is required' };
} }
}, null); }, null);
print(' led.putLamps'); print(' led.putLamps');