Resistor Color Code Calculator (Band to Value)

4-Band
5-Band

Resistance:

Reverse Resistor Calculator (Value to Band Colors)

Enter resistance and get the corresponding color bands.

Ω

±1%
±2%
±5%
±10%

4-Band
5-Band

const colorCodes = [
{ name: “Black”, value: 0, multiplier: 1, hex: “#000000” },
{ name: “Brown”, value: 1, multiplier: 10, tolerance: “±1%”, hex: “#8B4513” },
{ name: “Red”, value: 2, multiplier: 100, tolerance: “±2%”, hex: “#FF0000” },
{ name: “Orange”, value: 3, multiplier: 1e3, hex: “#FFA500” },
{ name: “Yellow”, value: 4, multiplier: 1e4, hex: “#FFFF00” },
{ name: “Green”, value: 5, multiplier: 1e5, tolerance: “±0.5%”, hex: “#008000” },
{ name: “Blue”, value: 6, multiplier: 1e6, tolerance: “±0.25%”, hex: “#0000FF” },
{ name: “Violet”, value: 7, multiplier: 1e7, tolerance: “±0.1%”, hex: “#EE82EE” },
{ name: “Gray”, value: 8, multiplier: 1e8, tolerance: “±0.05%”, hex: “#808080” },
{ name: “White”, value: 9, multiplier: 1e9, hex: “#FFFFFF” },
];

const toleranceCodes = [
{ name: “Gold”, tolerance: “±5%”, hex: “#FFD700” },
{ name: “Silver”, tolerance: “±10%”, hex: “#C0C0C0” },
{ name: “None”, tolerance: “±20%”, hex: “#F0F0F0” }
];

function switchBandType() {
const bandType = parseInt(document.getElementById(“bandType”).value);
const selectorArea = document.getElementById(“selectorArea”);
const colorDisplay = document.getElementById(“colorDisplay”);

selectorArea.innerHTML = “”;
colorDisplay.innerHTML = “”;

let labels = bandType === 4 ? [“Band 1”, “Band 2”, “Multiplier”, “Tolerance”] :
[“Band 1”, “Band 2”, “Band 3”, “Multiplier”, “Tolerance”];

labels.forEach((label, i) => {
const sel = document.createElement(“select”);
sel.id = “band” + (i + 1);
sel.onchange = calculateResistor;

const wrapper = document.createElement(“div”);
wrapper.innerHTML = `
`;
wrapper.appendChild(sel);
selectorArea.appendChild(wrapper);

const strip = document.createElement(“div”);
strip.id = “strip” + (i + 1);
strip.style = “width: 30px; height: 100px; background: #eee;”;
colorDisplay.appendChild(strip);
});

populateSelectOptions();
calculateResistor();
}

function populateSelectOptions() {
const bandType = parseInt(document.getElementById(“bandType”).value);
const totalBands = bandType;

for (let i = 1; i <= totalBands; i++) {
const sel = document.getElementById("band" + i);
sel.innerHTML = '–Select–‘;

if ((bandType === 4 && i === 4) || (bandType === 5 && i === 5)) {
[…colorCodes, …toleranceCodes].forEach(c => {
if (c.tolerance) {
sel.innerHTML += `${c.name}`;
}
});
} else if (bandType === 5 && i === 4 || bandType === 4 && i === 3) {
colorCodes.forEach(c => {
sel.innerHTML += `${c.name}`;
});
} else {
colorCodes.forEach(c => {
sel.innerHTML += `${c.name}`;
});
}
}
}

function calculateResistor() {
const bandType = parseInt(document.getElementById(“bandType”).value);

const getColor = i => {
const el = document.getElementById(“band” + i);
const color = el.options[el.selectedIndex]?.getAttribute(“data-color”);
document.getElementById(“strip” + i).style.background = color || “#eee”;
return el.value;
};

if (bandType === 4) {
const d1 = parseInt(getColor(1));
const d2 = parseInt(getColor(2));
const multiplier = parseFloat(getColor(3));
const tol = getColor(4);

if (!isNaN(d1) && !isNaN(d2) && !isNaN(multiplier)) {
let res = (d1 * 10 + d2) * multiplier;
document.getElementById(“resValue”).innerText = formatRes(res) + ” ” + tol;
} else {
document.getElementById(“resValue”).innerText = “–“;
}
}

if (bandType === 5) {
const d1 = parseInt(getColor(1));
const d2 = parseInt(getColor(2));
const d3 = parseInt(getColor(3));
const multiplier = parseFloat(getColor(4));
const tol = getColor(5);

if (!isNaN(d1) && !isNaN(d2) && !isNaN(d3) && !isNaN(multiplier)) {
let res = (d1 * 100 + d2 * 10 + d3) * multiplier;
document.getElementById(“resValue”).innerText = formatRes(res) + ” ” + tol;
} else {
document.getElementById(“resValue”).innerText = “–“;
}
}
}

function formatRes(res) {
if (res >= 1e6) return (res / 1e6).toFixed(2) + ” MΩ”;
if (res >= 1e3) return (res / 1e3).toFixed(2) + ” kΩ”;
return res.toFixed(2) + ” Ω”;
}

// Reverse Calculation
function reverseCalculate() {
const val = parseFloat(document.getElementById(“reverseValue”).value) * parseFloat(document.getElementById(“reverseUnit”).value);
const tol = document.getElementById(“reverseTolerance”).value;
const bands = parseInt(document.getElementById(“reverseBands”).value);
const output = document.getElementById(“reverseOutput”);

if (isNaN(val)) {
output.innerHTML = “Enter valid resistance value.”;
return;
}

let digits = val.toPrecision(bands === 4 ? 2 : 3).replace(“.”, “”).slice(0, bands === 4 ? 2 : 3);
let multiplier = val / parseInt(digits);

const findColor = (type, match) => {
let arr = type === “tolerance” ? toleranceCodes : colorCodes;
return arr.find(c => (type === “multiplier” ? c.multiplier : c.tolerance) == match)?.name || “?”;
};

let result = `

`;
for (let i = 0; i < digits.length; i++) {
let digit = parseInt(digits[i]);
result += `

`;
}

let multiColor = colorCodes.find(c => c.multiplier == Math.round(multiplier));
result += `

`;

let tolColor = […colorCodes, …toleranceCodes].find(c => c.tolerance == tol);
result += `

`;
result += `

Colors: ${digits.split(“”).map(d => colorCodes[parseInt(d)].name).join(“, “)}, ${multiColor?.name}, ${tolColor?.name}

`;

output.innerHTML = result;
}

switchBandType(); // Initializer


(adsbygoogle = window.adsbygoogle || []).push({});