Fix capitalization. Improve decimal handling.

master
stweedo 2023-07-29 18:02:16 -05:00
parent ddc9ac34b1
commit 459db44c4a
3 changed files with 71 additions and 63 deletions

View File

@ -1,3 +1,4 @@
0.01: New App! 0.01: New App!
0.02: Fixes colors not matching user input from color menu in some cases, 3 bands are now shown larger, various code improvements. 0.02: Fixes colors not matching user input from color menu in some cases, 3 bands are now shown larger, various code improvements.
0.03: Use transparent icon with better visibility on dark backgrounds, new resistor img with darker outlines 0.03: Use transparent icon with better visibility on dark backgrounds, new resistor img with darker outlines
0.04: Fix capitalization. Improve decimal handling.

View File

@ -3,19 +3,19 @@
// https://icons8.com/icon/ISAVBnskZod0/resistor // https://icons8.com/icon/ISAVBnskZod0/resistor
let colorData = { let colorData = {
black: { value: 0, multiplier: 1, hex: '#000' }, Black: { value: 0, multiplier: 1, hex: '#000' },
brown: { value: 1, multiplier: 10, tolerance: 1, hex: '#8B4513' }, Brown: { value: 1, multiplier: 10, tolerance: 1, hex: '#8B4513' },
red: { value: 2, multiplier: 100, tolerance: 2, hex: '#f00' }, Red: { value: 2, multiplier: 100, tolerance: 2, hex: '#f00' },
orange: { value: 3, multiplier: 1000, hex: '#FF9900' }, Orange: { value: 3, multiplier: 1000, hex: '#FF9900' },
yellow: { value: 4, multiplier: 10000, hex: '#ff0' }, Yellow: { value: 4, multiplier: 10000, hex: '#ff0' },
green: { value: 5, multiplier: 100000, tolerance: 0.5, hex: '#0f0' }, Green: { value: 5, multiplier: 100000, tolerance: 0.5, hex: '#0f0' },
blue: { value: 6, multiplier: 1000000, tolerance: 0.25, hex: '#00f' }, Blue: { value: 6, multiplier: 1000000, tolerance: 0.25, hex: '#00f' },
violet: { value: 7, multiplier: 10000000, tolerance: 0.1, hex: '#f0f' }, Violet: { value: 7, multiplier: 10000000, tolerance: 0.1, hex: '#f0f' },
grey: { value: 8, multiplier: 100000000, tolerance: 0.05, hex: '#808080' }, Grey: { value: 8, multiplier: 100000000, tolerance: 0.05, hex: '#808080' },
white: { value: 9, multiplier: 1000000000, hex: '#fff' }, White: { value: 9, multiplier: 1000000000, hex: '#fff' },
gold: { multiplier: 0.1, tolerance: 5, hex: '#FFD700' }, Gold: { multiplier: 0.1, tolerance: 5, hex: '#FFD700' },
silver: { multiplier: 0.01, tolerance: 10, hex: '#C0C0C0' }, Silver: { multiplier: 0.01, tolerance: 10, hex: '#C0C0C0' },
none: { tolerance: 20 }, None: { tolerance: 20 },
}; };
function clearScreen() { // Except Back Button function clearScreen() { // Except Back Button
@ -27,7 +27,7 @@ function colorBandsToResistance(colorBands) {
let firstBand = colorBands[0]; let firstBand = colorBands[0];
let secondBand = colorBands[1]; let secondBand = colorBands[1];
let multiplierBand = colorBands[2]; let multiplierBand = colorBands[2];
let toleranceBand = colorBands[3] || 'none'; // Add a default value for toleranceBand let toleranceBand = colorBands[3] || 'None'; // Add a default value for toleranceBand
let significantDigits = colorData[firstBand].value * 10 + colorData[secondBand].value; let significantDigits = colorData[firstBand].value * 10 + colorData[secondBand].value;
let multiplier = colorData[multiplierBand].multiplier; let multiplier = colorData[multiplierBand].multiplier;
let resistance = significantDigits * multiplier; let resistance = significantDigits * multiplier;
@ -35,57 +35,65 @@ function colorBandsToResistance(colorBands) {
return [resistance, tolerance]; return [resistance, tolerance];
} }
// Function to get color bands based on resistance and tolerance
function resistanceToColorBands(resistance, tolerance) { function resistanceToColorBands(resistance, tolerance) {
let firstDigit, secondDigit, multiplier; let firstDigit, secondDigit, multiplier;
if (resistance < 1) {
// The resistance is less than 1, so we need to handle this case specially
let count = 0;
while (resistance < 1) {
resistance *= 10;
count++;
}
// Now, resistance is a whole number and count is how many times we had to multiply by 10
let resistanceStr = resistance.toString(); let resistanceStr = resistance.toString();
firstDigit = 0; // Set the first band color to be black let decimalIndex = resistanceStr.indexOf('.');
secondDigit = Number(resistanceStr.charAt(0)); // Set the second band color to be the significant digit
// Use count to determine the multiplier // Handle resistance with decimal
multiplier = count === 1 ? 0.1 : 0.01; if (decimalIndex !== -1) {
} else { let integerDigits = resistanceStr.substring(0, decimalIndex);
// Convert the resistance to a string so we can manipulate it easily let decimalDigits = resistanceStr.substring(decimalIndex + 1);
let resistanceStr = resistance.toString(); let leadingZeros = decimalDigits.match(/^0*/)[0].length;
if (resistanceStr.length === 1) { // Check if resistance is a single digit
// If resistance is less than 1
if (parseInt(integerDigits) === 0) {
if (leadingZeros === decimalDigits.length - 1) {
// If only one significant digit
firstDigit = 0; firstDigit = 0;
secondDigit = Number(resistanceStr.charAt(0)); secondDigit = parseInt(decimalDigits.charAt(leadingZeros));
multiplier = 1; // Set multiplier to 1 for single digit resistance values multiplier = 1 / Math.pow(10, leadingZeros + 1);
} else { } else {
// Extract the first two digits from the resistance value // If more than one significant digit
firstDigit = Number(resistanceStr.charAt(0)); firstDigit = parseInt(decimalDigits.charAt(leadingZeros));
secondDigit = Number(resistanceStr.charAt(1)); secondDigit = parseInt(decimalDigits.charAt(leadingZeros + 1));
// Calculate the multiplier by matching it directly with the length of digits multiplier = 1 / Math.pow(10, leadingZeros + 2);
multiplier = resistanceStr.length - 2 >= 0 ? Math.pow(10, resistanceStr.length - 2) : Math.pow(10, resistanceStr.length - 1);
} }
} else {
// If resistance is greater than 1
firstDigit = parseInt(integerDigits.charAt(0));
secondDigit = parseInt(decimalDigits.charAt(0));
multiplier = 1 / Math.pow(10, decimalDigits.length);
} }
let firstBandEntry = Object.entries(colorData).find(function (entry) { } else {
return entry[1].value === firstDigit; // Handle resistance without decimal
}); firstDigit = resistanceStr.length === 1 ? 0 : parseInt(resistanceStr.charAt(0));
let firstBand = firstBandEntry ? firstBandEntry[1].hex : undefined; secondDigit = parseInt(resistanceStr.charAt(resistanceStr.length === 1 ? 0 : 1));
let secondBandEntry = Object.entries(colorData).find(function (entry) { multiplier = Math.pow(10, resistanceStr.length - 2);
return entry[1].value === secondDigit; }
});
let secondBand = secondBandEntry ? secondBandEntry[1].hex : undefined; // Generate color bands array
let multiplierBandEntry = Object.entries(colorData).find(function (entry) { let bands = [
return entry[1].multiplier === multiplier; getBandColor('value', firstDigit),
}); getBandColor('value', secondDigit),
let multiplierBand = multiplierBandEntry ? multiplierBandEntry[1].hex : undefined; getBandColor('multiplier', multiplier),
let toleranceBandEntry = Object.entries(colorData).find(function (entry) { ];
return entry[1].tolerance === tolerance;
}); // Add tolerance color band if provided
let toleranceBand = toleranceBandEntry ? toleranceBandEntry[1].hex : undefined; let toleranceBand = getBandColor('tolerance', tolerance);
let bands = [firstBand, secondBand, multiplierBand];
if (toleranceBand) bands.push(toleranceBand); if (toleranceBand) bands.push(toleranceBand);
return bands; return bands;
} }
// Helper function to get color band based on property and value
function getBandColor(property, value) {
let entry = Object.entries(colorData).find(function (entry) {
return entry[1][property] === value;
});
return entry ? entry[1].hex : undefined;
}
function drawResistor(colorBands, tolerance) { function drawResistor(colorBands, tolerance) {
let img = require("Storage").read("rescalc-resistor.img"); let img = require("Storage").read("rescalc-resistor.img");
let resistorBodyWidth = 51; let resistorBodyWidth = 51;
@ -208,17 +216,17 @@ function drawResistance(resistance, tolerance) {
// Populate colorBandMenu with colors from colorData // Populate colorBandMenu with colors from colorData
for (let color in colorData) { for (let color in colorData) {
if (bandNumber === 1 || bandNumber === 2) { if (bandNumber === 1 || bandNumber === 2) {
if (color !== 'none' && color !== 'gold' && color !== 'silver') { if (color !== 'None' && color !== 'Gold' && color !== 'Silver') {
(function (color) { (function (color) {
colorBandMenu[color.charAt(0).toUpperCase() + color.slice(1)] = function () { colorBandMenu[color] = function () {
setBandColor(bandNumber, color); setBandColor(bandNumber, color);
}; };
})(color); })(color);
} }
} else if (bandNumber === 3) { } else if (bandNumber === 3) {
if (color !== 'none') { if (color !== 'None') {
(function (color) { (function (color) {
colorBandMenu[color.charAt(0).toUpperCase() + color.slice(1)] = function () { colorBandMenu[color] = function () {
setBandColor(bandNumber, color); setBandColor(bandNumber, color);
}; };
})(color); })(color);
@ -226,7 +234,7 @@ function drawResistance(resistance, tolerance) {
} else if (bandNumber === 4) { } else if (bandNumber === 4) {
if (colorData[color].hasOwnProperty('tolerance')) { if (colorData[color].hasOwnProperty('tolerance')) {
(function (color) { (function (color) {
colorBandMenu[color.charAt(0).toUpperCase() + color.slice(1)] = function () { colorBandMenu[color] = function () {
setBandColor(bandNumber, color); setBandColor(bandNumber, color);
}; };
})(color); })(color);
@ -407,7 +415,6 @@ function drawResistance(resistance, tolerance) {
}; };
function showResistanceEntryMenu() { function showResistanceEntryMenu() {
// Update the 'Ohms' field
resistanceEntryMenu['Ohms'].value = settings.resistance; resistanceEntryMenu['Ohms'].value = settings.resistance;
resistanceEntryMenu['Ohms'].format = v => { resistanceEntryMenu['Ohms'].format = v => {
let multipliedValue = v * (settings.multiplier || 1); let multipliedValue = v * (settings.multiplier || 1);

View File

@ -3,7 +3,7 @@
"name": "Resistor Calculator", "name": "Resistor Calculator",
"shortName": "Resistor Calc", "shortName": "Resistor Calc",
"icon": "rescalc.png", "icon": "rescalc.png",
"version":"0.03", "version":"0.04",
"screenshots": [ "screenshots": [
{"url": "screenshot.png"}, {"url": "screenshot.png"},
{"url": "screenshot-1.png"}, {"url": "screenshot-1.png"},