-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
329 lines (289 loc) · 14.6 KB
/
script.js
File metadata and controls
329 lines (289 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
Resistor Color Code Calculator - JavaScript
Author: Dimitrios Poulos
Description: Handles SVG preview updates, band visibility and resistance calculation logic.
*/
/* Color Mapping */
var band_colors =
{
"0":"#222222", "1":"#8B4513", "2":"#cc2200",
"3":"#ff6600", "4":"#ccaa00", "5":"#2d8c2d",
"6":"#1a5fb5", "7":"#7a4fbe", "8":"#888888",
"9":"#e0e0e0",
"10":"#8B4513", "100":"#cc2200", "1000":"#ff6600",
"10000":"#ccaa00", "100000":"#2d8c2d", "1000000":"#1a5fb5",
"10000000":"#7a4fbe", "100000000":"#888888", "1000000000":"#e0e0e0",
"0.1":"#d4af37", "0.01":"#c0c0c0"
};
// Function that updates the fill color of each SVG band based on the current dropdown selections. Also shows/hides bands 4, 5, 6 based on the number of bands.
function update_svg()
{
var bands_number = parseInt(document.getElementById("bands_number").value);
// Updates the first 3 bands (always visible) if selected.
if(document.getElementById("band1").value != "")
{
document.getElementById("svg_band1").setAttribute("fill", band_colors[document.getElementById("band1").value]);
document.getElementById("band1").style.borderColor = band_colors[document.getElementById("band1").value];
}
if(document.getElementById("band2").value != "")
{
document.getElementById("svg_band2").setAttribute("fill", band_colors[document.getElementById("band2").value]);
document.getElementById("band2").style.borderColor = band_colors[document.getElementById("band2").value];
}
if(document.getElementById("band3").value != "")
{
document.getElementById("svg_band3").setAttribute("fill", band_colors[document.getElementById("band3").value]);
document.getElementById("band3").style.borderColor = band_colors[document.getElementById("band3").value];
}
// Resets optional SVG bands to hidden.
document.getElementById("svg_band4").style.display = "none";
document.getElementById("svg_band5").style.display = "none";
document.getElementById("svg_band6").style.display = "none";
// Paints and displays optional bands based on user's choices.
if(bands_number == 4)
{
document.getElementById("svg_band4").style.display = "block";
if(document.getElementById("band4").value != "")
{
document.getElementById("svg_band4").setAttribute("fill", band_colors[document.getElementById("band4").value]);
document.getElementById("band4").style.borderColor = band_colors[document.getElementById("band4").value];
}
}
else if(bands_number == 5)
{
document.getElementById("svg_band4").style.display = "block";
if(document.getElementById("band4").value != "")
{
document.getElementById("svg_band4").setAttribute("fill", band_colors[document.getElementById("band4").value]);
document.getElementById("band4").style.borderColor = band_colors[document.getElementById("band4").value];
}
document.getElementById("svg_band5").style.display = "block";
if(document.getElementById("band5").value != "")
{
document.getElementById("svg_band5").setAttribute("fill", band_colors[document.getElementById("band5").value]);
document.getElementById("band5").style.borderColor = band_colors[document.getElementById("band5").value];
}
}
else if(bands_number == 6)
{
document.getElementById("svg_band4").style.display = "block";
if(document.getElementById("band4").value != "")
{
document.getElementById("svg_band4").setAttribute("fill", band_colors[document.getElementById("band4").value]);
document.getElementById("band4").style.borderColor = band_colors[document.getElementById("band4").value];
}
document.getElementById("svg_band5").style.display = "block";
if(document.getElementById("band5").value != "")
{
document.getElementById("svg_band5").setAttribute("fill", band_colors[document.getElementById("band5").value]);
document.getElementById("band5").style.borderColor = band_colors[document.getElementById("band5").value];
}
document.getElementById("svg_band6").style.display = "block";
if(document.getElementById("band6").value != "")
{
document.getElementById("svg_band6").setAttribute("fill", band_colors[document.getElementById("band6").value]);
document.getElementById("band6").style.borderColor = band_colors[document.getElementById("band6").value];
}
}
}
// Function that modifies the dropdown menus shown to the user based on the selected number of bands.
function update_bands()
{
var bands_number = parseInt(document.getElementById("bands_number").value);
// Hides all optional band rows.
document.getElementById("band4_list").style.display = "none";
document.getElementById("band5_list").style.display = "none";
document.getElementById("band6_list").style.display = "none";
if(bands_number == 3)
{
// Band 3 acts as Multiplier in 3-band resistor.
document.getElementById("band3_label").textContent = "3rd Band (Multiplier):";
document.getElementById("band3").innerHTML = `
<option value="" disabled selected>-</option>
<option value="1">Black (x1Ω)</option>
<option value="10">Brown (x10Ω)</option>
<option value="100">Red (x100Ω)</option>
<option value="1000">Orange (x1KΩ)</option>
<option value="10000">Yellow (x10KΩ)</option>
<option value="100000">Green (x100KΩ)</option>
<option value="1000000">Blue (x1MΩ)</option>
<option value="0.1">Gold (x0.1Ω)</option>
<option value="0.01">Silver (x0.01Ω)</option>
`;
}
else if(bands_number == 4)
{
// Band 3 acts as Multiplier and band 4 as Tolerance in 4-band resistor.
document.getElementById("band3_label").textContent = "3rd Band (Multiplier):";
document.getElementById("band3").innerHTML = `
<option value="" disabled selected>-</option>
<option value="1">Black (x1Ω)</option>
<option value="10">Brown (x10Ω)</option>
<option value="100">Red (x100Ω)</option>
<option value="1000">Orange (x1KΩ)</option>
<option value="10000">Yellow (x10KΩ)</option>
<option value="100000">Green (x100KΩ)</option>
<option value="1000000">Blue (x1MΩ)</option>
<option value="10000000">Violet (x10MΩ)</option>
<option value="100000000">Gray (x100MΩ)</option>
<option value="1000000000">White (x1GΩ)</option>
<option value="0.1">Gold (x0.1Ω)</option>
<option value="0.01">Silver (x0.01Ω)</option>
`;
document.getElementById("band4_label").textContent = "4th Band (Tolerance):";
document.getElementById("band4").innerHTML = `
<option value="" disabled selected>-</option>
<option value="±1%">Brown (±1%)</option>
<option value="±2%">Red (±2%)</option>
<option value="±0.5%">Green (±0.5%)</option>
<option value="±0.25%">Blue (±0.25%)</option>
<option value="±0.1%">Violet (±0.1%)</option>
<option value="±5%">Gold (±5%)</option>
<option value="±10%">Silver (±10%)</option>
`;
document.getElementById("band4_list").style.display = "flex";
}
else if(bands_number == 5)
{
// Band 3 acts as 3rd digit, band 4 as Multiplier and band 5 as Tolerance in a 5-band resistor.
document.getElementById("band3_label").textContent = "3rd Band (3rd Digit):";
document.getElementById("band3").innerHTML = `
<option value="" disabled selected>-</option>
<option value="0">Black (0)</option>
<option value="1">Brown (1)</option>
<option value="2">Red (2)</option>
<option value="3">Orange (3)</option>
<option value="4">Yellow (4)</option>
<option value="5">Green (5)</option>
<option value="6">Blue (6)</option>
<option value="7">Violet (7)</option>
<option value="8">Gray (8)</option>
<option value="9">White (9)</option>
`;
document.getElementById("band4_label").textContent = "4th Band (Multiplier):";
document.getElementById("band4").innerHTML = `
<option value="" disabled selected>-</option>
<option value="1">Black (x1Ω)</option>
<option value="10">Brown (x10Ω)</option>
<option value="100">Red (x100Ω)</option>
<option value="1000">Orange (x1KΩ)</option>
<option value="10000">Yellow (x10KΩ)</option>
<option value="100000">Green (x100KΩ)</option>
<option value="1000000">Blue (x1MΩ)</option>
<option value="0.1">Gold (x0.1Ω)</option>
<option value="0.01">Silver (x0.01Ω)</option>
`;
document.getElementById("band4_list").style.display = "flex";
document.getElementById("band5_list").style.display = "flex";
}
else if(bands_number == 6)
{
// Band 3 acts as 3rd digit, band 4 as Multiplier, band 5 as Tolerance and band 6 as Temperature Coefficient in a 6-band resistor.
document.getElementById("band3_label").textContent = "3rd Band (3rd Digit):";
document.getElementById("band3").innerHTML = `
<option value="" disabled selected>-</option>
<option value="0">Black (0)</option>
<option value="1">Brown (1)</option>
<option value="2">Red (2)</option>
<option value="3">Orange (3)</option>
<option value="4">Yellow (4)</option>
<option value="5">Green (5)</option>
<option value="6">Blue (6)</option>
<option value="7">Violet (7)</option>
<option value="8">Gray (8)</option>
<option value="9">White (9)</option>
`;
document.getElementById("band4_label").textContent = "4th Band (Multiplier):";
document.getElementById("band4").innerHTML = `
<option value="" disabled selected>-</option>
<option value="1">Black (x1Ω)</option>
<option value="10">Brown (x10Ω)</option>
<option value="100">Red (x100Ω)</option>
<option value="1000">Orange (x1KΩ)</option>
<option value="10000">Yellow (x10KΩ)</option>
<option value="100000">Green (x100KΩ)</option>
<option value="1000000">Blue (x1MΩ)</option>
<option value="0.1">Gold (x0.1Ω)</option>
<option value="0.01">Silver (x0.01Ω)</option>
`;
document.getElementById("band4_list").style.display = "flex";
document.getElementById("band5_list").style.display = "flex";
document.getElementById("band6_list").style.display = "flex";
}
update_svg();
}
// Function that formats large resistance values into more readable KΩ (Kilo-ohms) and MΩ (Mega-ohms).
function format_resistance(resistance)
{
if(resistance >= 1000000)
return resistance/1000000 + " MΩ (" + resistance + " Ω)";
else if(resistance >= 1000)
return resistance/1000 + " KΩ (" + resistance + " Ω)";
else
return resistance + " Ω";
}
// Function that calculates the resistance.
function calculate_resistance()
{
var bands_number = parseInt(document.getElementById("bands_number").value);
// Validates that bands 1, 2, 3 are selected.
if(document.getElementById("band1").value == "" || document.getElementById("band2").value == "" || document.getElementById("band3").value == "")
{
document.getElementById("resistance_results").textContent = "⚠ Please select all bands!";
return;
}
// Validates that optional bands (based on bands_number) are selected.
if(bands_number >= 4 && document.getElementById("band4").value == "")
{
document.getElementById("resistance_results").textContent = "⚠ Please select all bands!";
return;
}
if(bands_number >= 5 && document.getElementById("band5").value == "")
{
document.getElementById("resistance_results").textContent = "⚠ Please select all bands!";
return;
}
if(bands_number >= 6 && document.getElementById("band6").value == "")
{
document.getElementById("resistance_results").textContent = "⚠ Please select all bands!";
return;
}
// Start of calculations.
var digit1 = parseInt(document.getElementById("band1").value);
var digit2 = parseInt(document.getElementById("band2").value);
var multiplier, resistance;
if(bands_number == 3)
{
// 3-Band Formula: ((digit1 * 10) + digit2) * multiplier.
multiplier = parseFloat(document.getElementById("band3").value);
resistance = ((digit1 * 10) + digit2) * multiplier;
document.getElementById("resistance_results").textContent = format_resistance(resistance);
}
else if(bands_number == 4)
{
// 4-Band Formula: ((digit1 * 10) + digit2) * multiplier ± tolerance.
multiplier = parseFloat(document.getElementById("band3").value);
var tolerance = document.getElementById("band4").value;
resistance = ((digit1 * 10) + digit2) * multiplier;
document.getElementById("resistance_results").textContent = format_resistance(resistance) + " " + tolerance;
}
else if(bands_number == 5)
{
// 5-Band Formula: ((digit1 * 100) + (digit2 * 10) + digit3) * multiplier ± tolerance.
var digit3 = parseInt(document.getElementById("band3").value);
multiplier = parseFloat(document.getElementById("band4").value);
var tolerance = document.getElementById("band5").value;
resistance = ((digit1 * 100) + (digit2 * 10) + digit3) * multiplier;
document.getElementById("resistance_results").textContent = format_resistance(resistance) + " " + tolerance;
}
else if(bands_number == 6)
{
// 6-Band Formula: ((digit1 * 100) + (digit2 * 10) + digit3) * multiplier ± tolerance, temp coef.
var digit3 = parseInt(document.getElementById("band3").value);
multiplier = parseFloat(document.getElementById("band4").value);
var tolerance = document.getElementById("band5").value;
var temp_coef = document.getElementById("band6").value;
resistance = ((digit1 * 100) + (digit2 * 10) + digit3) * multiplier;
document.getElementById("resistance_results").textContent = format_resistance(resistance) + " " + tolerance + " " + temp_coef;
}
}