Changed color definition to use hex strings
This commit is contained in:
57
fs/init.js
57
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<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() {
|
||||
// Load Color definitions
|
||||
let json = File.read(colorFile);
|
||||
@@ -109,7 +157,10 @@ function loadColorDefs() {
|
||||
colors = JSON.parse(json);
|
||||
}
|
||||
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);
|
||||
}
|
||||
json = null;
|
||||
@@ -327,11 +378,11 @@ function recoverFile(filename) {
|
||||
}
|
||||
print(' led.putColors');
|
||||
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);
|
||||
return { result: 'ok' };
|
||||
} else {
|
||||
return { error: 'colors: {name, red, green, blue} is required' };
|
||||
return { error: 'colors: {name, color} is required' };
|
||||
}
|
||||
}, null);
|
||||
print(' led.putLamps');
|
||||
|
||||
Reference in New Issue
Block a user