feat(new tool): Resistor Data Calculator

Fix #580
This commit is contained in:
ShareVB committed 2025-08-23 15:28:10 +02:00
1 parent dd87f353aa
commit 900c4c9266
5 files changed
+304

No files matched your search

+1
View File
@@ -266,6 +266,7 @@
"randexp": "^0.5.3",
"regex": "^4.4.0",
"rekcod": "^3.0.0",
"resistor-data": "^1.1.1",
"riimut": "^0.9.1",
"roboto-base64": "^0.1.2",
"rtf-stream-parser": "^3.8.0",
+7
View File
@@ -686,6 +686,9 @@ dependencies:
rekcod:
specifier: ^3.0.0
version: 3.0.0
resistor-data:
specifier: ^1.1.1
version: 1.1.1
riimut:
specifier: ^0.9.1
version: 0.9.1
@@ -16557,6 +16560,10 @@ packages:
resolution: {integrity: sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==}
dev: true
/resistor-data@1.1.1:
resolution: {integrity: sha512-J8cwe8A4JWYhCM1qjPEp3Ccd2SY+h/yVGeULfpWceGYniJPluv0FbhqgTuJMgl2W0QcUngskckX6mIsTQLnoiA==}
dev: false
/resolve-dir@1.0.1:
resolution: {integrity: sha512-R7uiTjECzvOsWSfdM0QKFNBVFcK27aHOUwdvK53BcW8zqnGdYp0Fbj82cy54+2A4P2tFM22J5kRfe1R+lM/1yg==}
engines: {node: '>=0.10.0'}
+13
View File
@@ -0,0 +1,13 @@
import { Hammer } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Resistor Calculator',
path: '/resistor-calculator',
description: 'Compute Resistor values from/to band colors',
keywords: ['resistor', 'ohm', 'calculator'],
component: () => import('./resistor-calculator.vue'),
icon: Hammer,
createdAt: new Date('2025-08-15'),
category: 'Forensic',
});
@@ -0,0 +1,277 @@
<script setup lang="ts">
import { ref } from 'vue';
import { bandsToNotation, notationToBands, notationToValue, valueToNotation } from 'resistor-data';
const bandsInput = ref('');
const bandsResult = ref('');
const bandPreview = ref<string[]>([]);
const bandsError = ref('');
const bandCount = ref(4);
const bandOptions = [
{ label: '4-band', value: 4 },
{ label: '5-band', value: 5 },
{ label: '6-band', value: 6 },
];
const layout = ref('axial');
const notationInput = ref('');
const notationBandCount = ref(4);
const notationBandsResult = ref<string[]>([]);
const notationError = ref('');
const numberInput = ref(null);
const numberNotationResult = ref('');
const numberError = ref('');
const tolerance = ref(5);
const toleranceOptions = [
{ label: '±1%', value: 1 },
{ label: '±2%', value: 2 },
{ label: '±5%', value: 5 },
{ label: '±10%', value: 10 },
];
const notationToNumberInput = ref('');
const notationToNumberResult = ref(0);
const notationToNumberError = ref('');
const validColors = [
'black', 'brown', 'red', 'orange', 'yellow', 'green',
'blue', 'violet', 'gray', 'white', 'gold', 'silver',
];
function validateBands(bands: string[], count: number) {
return bands.length === count && bands.every(b => validColors.includes(b));
}
function convertBandsToNotation() {
bandsError.value = '';
bandsResult.value = '';
bandPreview.value = [];
const bands = bandsInput.value.split(',').map(b => b.trim().toLowerCase());
if (!validateBands(bands, bandCount.value)) {
bandsError.value = `Please enter exactly ${bandCount.value} valid color bands (${validColors.join(', ')}).`;
return;
}
try {
const [notation, tolerance] = bandsToNotation(bands, bands.length);
bandsResult.value = `${notation}Ω ±${tolerance}%`;
bandPreview.value = bands;
}
catch (e: any) {
bandsError.value = `Conversion failed. Check band order and values: ${e.toString()}`;
}
}
function convertNotationToBands() {
notationError.value = '';
notationBandsResult.value = [];
try {
const [notation, tolerance] = notationInput.value.replace(/[Ω±%]/g, '').replace(/\s+/g, ' ').trim().split(' ');
const bands = notationToBands([notation, Number(tolerance) || 1], notationBandCount.value);
notationBandsResult.value = bands;
}
catch (e: any) {
notationError.value = `Invalid notation format: ${e.toString()}`;
}
}
function convertNumberToNotation() {
numberError.value = '';
numberNotationResult.value = '';
if (typeof numberInput.value !== 'number' || numberInput.value <= 0) {
numberError.value = 'Please enter a valid positive number.';
return;
}
try {
numberNotationResult.value = `${valueToNotation(numberInput.value)}Ω ±${tolerance.value}%`;
}
catch (e: any) {
numberError.value = `Conversion failed: ${e.toString()}`;
}
}
function convertNotationToNumber() {
notationToNumberError.value = '';
notationToNumberResult.value = 0;
try {
notationToNumberResult.value = notationToValue(notationToNumberInput.value);
}
catch (e: any) {
notationToNumberError.value = `Invalid notation format: ${e.toString()}`;
}
}
function getColorHex(color: string) {
const map = { black: '#000000', brown: '#8B4513', red: '#FF0000', orange: '#FFA500', yellow: '#FFFF00', green: '#008000', blue: '#0000FF', violet: '#8A2BE2', gray: '#808080', white: '#FFFFFF', gold: '#FFD700', silver: '#C0C0C0' };
return map[color as keyof typeof map] || '#ccc';
}
</script>
<template>
<n-card title="Resistor Converter" style="max-width: 700px; margin: auto;">
<n-tabs type="line" animated>
<!-- Bands → Notation -->
<n-tab-pane name="bands-to-notation" tab="Bands → Notation">
<n-select v-model:value="bandCount" mb-1 :options="bandOptions" label="Band Count" style="margin-bottom: 10px" />
<n-input v-model:value="bandsInput" placeholder="e.g. red, violet, brown, gold" />
<n-p mb-2>
Valid colors: {{ validColors.join(', ') }}
</n-p>
<n-space justify="center" mb-2>
<n-button @click="convertBandsToNotation">
Convert
</n-button>
</n-space>
<n-alert v-if="bandsError" type="error">
{{ bandsError }}
</n-alert>
<n-result v-if="bandsResult" mb-2 status="success" title="Standard Notation" :description="bandsResult" />
<n-space v-if="bandPreview.length" justify="center" mb-2>
<n-tag v-for="(color, index) in bandPreview" :key="index" :style="{ 'background-color': getColorHex(color) }" round>
{{ color }}
</n-tag>
</n-space>
<n-space justify="center" mb-1>
<n-radio-group v-model:value="layout">
<n-radio-button value="axial">
Axial
</n-radio-button>
<n-radio-button value="radial">
Radial
</n-radio-button>
</n-radio-group>
</n-space>
<n-space justify="center" mb-1>
<svg width="300" height="100" style="margin-top: 20px">
<template v-if="layout === 'axial'">
<rect x="50" y="40" width="200" height="20" rx="10" fill="#ccc" />
<rect
v-for="(color, index) in bandPreview"
:key="index"
:x="60 + index * 30"
y="40"
width="10"
height="20"
:fill="getColorHex(color)"
/>
<line x1="30" y1="50" x2="50" y2="50" stroke="#888" stroke-width="2" />
<line x1="250" y1="50" x2="270" y2="50" stroke="#888" stroke-width="2" />
</template>
<template v-else>
<circle cx="150" cy="50" r="30" fill="#ccc" />
<rect
v-for="(color, index) in bandPreview"
:key="index"
:x="135 + index * 10"
y="30"
width="8"
height="40"
:fill="getColorHex(color)"
/>
<line x1="150" y1="20" x2="150" y2="0" stroke="#888" stroke-width="2" />
<line x1="150" y1="80" x2="150" y2="100" stroke="#888" stroke-width="2" />
</template>
</svg>
</n-space>
</n-tab-pane>
<!-- Notation → Bands -->
<n-tab-pane name="notation-to-bands" tab="Notation → Bands">
<n-input v-model:value="notationInput" placeholder="e.g. 2k7Ω ±5% or 2k7 5%" mb-2 />
<n-select v-model:value="notationBandCount" mb-1 :options="bandOptions" label="Band Count" style="margin-bottom: 10px" />
<n-space justify="center" mb-1>
<n-button @click="convertNotationToBands">
Convert
</n-button>
</n-space>
<n-alert v-if="notationError" type="error" mb-1>
{{ notationError }}
</n-alert>
<n-result v-if="notationBandsResult.length" mb-1 status="success" title="Resistor Bands" :description="`Result: ${notationBandsResult.join(', ')}`" />
<n-space v-if="notationBandsResult.length" justify="center" mb-3>
<n-tag v-for="(color, index) in notationBandsResult" :key="index" :style="{ 'background-color': getColorHex(color) }" round>
{{ color }}
</n-tag>
</n-space>
<n-space justify="center" mb-1>
<n-radio-group v-model:value="layout">
<n-radio-button value="axial">
Axial
</n-radio-button>
<n-radio-button value="radial">
Radial
</n-radio-button>
</n-radio-group>
</n-space>
<n-space justify="center" mb-1>
<svg width="300" height="100" style="margin-top: 20px">
<template v-if="layout === 'axial'">
<rect x="50" y="40" width="200" height="20" rx="10" fill="#ccc" />
<rect
v-for="(color, index) in notationBandsResult"
:key="index"
:x="60 + index * 30"
y="40"
width="10"
height="20"
:fill="getColorHex(color)"
/>
<line x1="30" y1="50" x2="50" y2="50" stroke="#888" stroke-width="2" />
<line x1="250" y1="50" x2="270" y2="50" stroke="#888" stroke-width="2" />
</template>
<template v-else>
<circle cx="150" cy="50" r="30" fill="#ccc" />
<rect
v-for="(color, index) in notationBandsResult"
:key="index"
:x="135 + index * 10"
y="30"
width="8"
height="40"
:fill="getColorHex(color)"
/>
<line x1="150" y1="20" x2="150" y2="0" stroke="#888" stroke-width="2" />
<line x1="150" y1="80" x2="150" y2="100" stroke="#888" stroke-width="2" />
</template>
</svg>
</n-space>
</n-tab-pane>
<!-- Number → Notation -->
<n-tab-pane name="number-to-notation" tab="Number → Notation">
<n-input-number v-model:value="numberInput" placeholder="e.g. 2700" mb-1 />
<n-select v-model:value="tolerance" :options="toleranceOptions" label="Tolerance" mb-1 />
<n-space justify="center" mb-1>
<n-button @click="convertNumberToNotation">
Convert
</n-button>
</n-space>
<n-alert v-if="numberError" type="error">
{{ numberError }}
</n-alert>
<n-result v-if="numberNotationResult" status="success" title="Standard Notation" :description="`Result: ${numberNotationResult}`" />
</n-tab-pane>
<!-- Notation → Number -->
<n-tab-pane name="notation-to-number" tab="Notation → Number">
<n-input v-model:value="notationToNumberInput" placeholder="e.g. 2k7Ω ±5% or 2k7 5%" />
<n-space justify="center" mb-1>
<n-button @click="convertNotationToNumber">
Convert
</n-button>
</n-space>
<n-alert v-if="notationToNumberError" type="error">
{{ notationToNumberError }}
</n-alert>
<n-result v-if="notationToNumberResult !== null" status="success" title="Numeric Value" :description="`Result: ${notationToNumberResult}Ω`" />
</n-tab-pane>
</n-tabs>
</n-card>
</template>
+6
View File
@@ -0,0 +1,6 @@
declare module 'resistor-data' {
export function bandsToNotation(bands: string[], bandsCount: number): any[];
export function notationToBands(notation:any[], bandsCount: number): string[];
export function notationToValue(notation: string): number;
export function valueToNotation(value: number): string;
}