[rescalc] - bugfix with decimal values to colors

pull/2797/head
stweedo 2023-06-05 00:31:08 -05:00
parent b4abf8571c
commit 2f0f5e8dd7
1 changed files with 31 additions and 18 deletions

View File

@ -36,25 +36,35 @@ function colorBandsToResistance(colorBands) {
} }
function resistanceToColorBands(resistance, tolerance) { function resistanceToColorBands(resistance, tolerance) {
let resistanceStr = resistance.toString();
let firstDigit, secondDigit, multiplier; let firstDigit, secondDigit, multiplier;
if (resistanceStr.length === 1) { // Check if resistance is a single digit if (resistance < 1) {
firstDigit = 0; // The resistance is less than 1, so we need to handle this case specially
secondDigit = Number(resistanceStr.charAt(0)); let count = 0;
multiplier = 0; while (resistance < 1) {
} else if (resistance >= 100) { resistance *= 10;
// Extract the first two digits from the resistance value count++;
firstDigit = Number(resistanceStr.charAt(0)); }
secondDigit = Number(resistanceStr.charAt(1)); // Now, resistance is a whole number and count is how many times we had to multiply by 10
// Calculate the multiplier let resistanceStr = resistance.toString();
multiplier = resistanceStr.length - 2; firstDigit = 0; // Set the first band color to be black
secondDigit = Number(resistanceStr.charAt(0)); // Set the second band color to be the significant digit
// Use count to determine the multiplier
multiplier = count === 1 ? 0.1 : 0.01;
} else { } else {
// For values between 10-99, shift the color to the first band // Convert the resistance to a string so we can manipulate it easily
firstDigit = Number(resistanceStr.charAt(0)); let resistanceStr = resistance.toString();
secondDigit = Number(resistanceStr.charAt(1)); if (resistanceStr.length === 1) { // Check if resistance is a single digit
multiplier = 0; firstDigit = 0;
secondDigit = Number(resistanceStr.charAt(0));
multiplier = 1; // Set multiplier to 1 for single digit resistance values
} else {
// Extract the first two digits from the resistance value
firstDigit = Number(resistanceStr.charAt(0));
secondDigit = Number(resistanceStr.charAt(1));
// Calculate the multiplier by matching it directly with the length of digits
multiplier = resistanceStr.length - 2 >= 0 ? Math.pow(10, resistanceStr.length - 2) : Math.pow(10, resistanceStr.length - 1);
}
} }
let firstBandEntry = Object.entries(colorData).find(function(entry) { let firstBandEntry = Object.entries(colorData).find(function(entry) {
return entry[1].value === firstDigit; return entry[1].value === firstDigit;
}); });
@ -64,7 +74,7 @@ function resistanceToColorBands(resistance, tolerance) {
}); });
let secondBand = secondBandEntry ? secondBandEntry[1].hex : undefined; let secondBand = secondBandEntry ? secondBandEntry[1].hex : undefined;
let multiplierBandEntry = Object.entries(colorData).find(function(entry) { let multiplierBandEntry = Object.entries(colorData).find(function(entry) {
return entry[1].multiplier === Math.pow(10, multiplier); return entry[1].multiplier === multiplier;
}); });
let multiplierBand = multiplierBandEntry ? multiplierBandEntry[1].hex : undefined; let multiplierBand = multiplierBandEntry ? multiplierBandEntry[1].hex : undefined;
let toleranceBandEntry = Object.entries(colorData).find(function(entry) { let toleranceBandEntry = Object.entries(colorData).find(function(entry) {
@ -362,8 +372,11 @@ function drawResistance(resistance, tolerance) {
} }
}); });
} else { } else {
console.log("Using colorBands = resistanceToColorBands(resistance, tolerance)" + "\nResistance: " + resistance + "\nTolerance: " + tolerance);
colorBands = resistanceToColorBands(resistance, tolerance); colorBands = resistanceToColorBands(resistance, tolerance);
} }
console.log(inputColorBands);
console.log(colorBands);
drawResistor(colorBands, tolerance); drawResistor(colorBands, tolerance);
drawResistance(resistance, tolerance); drawResistance(resistance, tolerance);
resetSettings(); resetSettings();