export function decodedResistorValue(colors: string[]): string { if (colors.length < 3) { throw new Error("Unexpected number of colors"); } // Ignore extra colors. const first = colors.slice(0, 2); let value = colorCode(first[0]) * 10 + colorCode(first[1]); let scale = colorCode(colors[2]); if (value % 10 === 0) { value /= 10; scale++; } let prefix: string = ''; switch (scale) { case 9: prefix = 'giga'; break; case 8: prefix = 'mega'; value *= 100; break; case 7: prefix = 'mega'; value *= 10; break; case 6: prefix = 'mega'; break; case 5: prefix = 'kilo'; value *= 100; break; case 4: prefix = 'kilo'; value *= 10; break; case 3: prefix = 'kilo'; break; case 2: value *= 100; break; case 1: value *= 10; break; } return `${value} ${prefix}ohms`; } export const colorCode = (name: string): number => { const num = COLORS.indexOf(name.toLowerCase()) if (num == -1) { throw new Error(`Invalid color name "${name}"`); } return num; } export const COLORS: string[] = [ 'black', 'brown', 'red', 'orange', 'yellow', 'green', 'blue', 'violet', 'grey', 'white', ];