@@ -2,18 +2,18 @@ import { useState } from "react";
2
2
import Head from "next/head" ;
3
3
4
4
const colors = [
5
- { label : "Black" , value : "black" } ,
6
- { label : "Brown" , value : "brown" } ,
7
- { label : "Red" , value : "red" } ,
8
- { label : "Orange" , value : "orange" } ,
9
- { label : "Yellow" , value : "yellow" } ,
10
- { label : "Green" , value : "green" } ,
11
- { label : "Blue" , value : "blue" } ,
12
- { label : "Violet" , value : "violet" } ,
13
- { label : "Gray" , value : "gray" } ,
14
- { label : "White" , value : "white" } ,
15
- { label : "Gold" , value : "gold" } ,
16
- { label : "Silver" , value : "silver" } ,
5
+ { label : "Black" , value : "black" , hexCode : "#1f2937" } ,
6
+ { label : "Brown" , value : "brown" , hexCode : "#a78b00" } ,
7
+ { label : "Red" , value : "red" , hexCode : "#ef4444" } ,
8
+ { label : "Orange" , value : "orange" , hexCode : "#f59e0b" } ,
9
+ { label : "Yellow" , value : "yellow" , hexCode : "#fcd34d" } ,
10
+ { label : "Green" , value : "green" , hexCode : "#10b981" } ,
11
+ { label : "Blue" , value : "blue" , hexCode : "#3b82f6" } ,
12
+ { label : "Violet" , value : "violet" , hexCode : "#8b5cf6" } ,
13
+ { label : "Gray" , value : "gray" , hexCode : "#6b7280" } ,
14
+ { label : "White" , value : "white" , hexCode : "#fff" } ,
15
+ { label : "Gold" , value : "gold" , hexCode : "#d1a038" } ,
16
+ { label : "Silver" , value : "silver" , hexCode : "#d8d8d8" } ,
17
17
] ;
18
18
19
19
const toleranceColors = [
@@ -77,9 +77,20 @@ const calculateResistance = (colors) => {
77
77
const value =
78
78
( colorToValue ( colors [ 0 ] ) * 10 + colorToValue ( colors [ 1 ] ) ) *
79
79
Math . pow ( 10 , colorToValue ( colors [ 2 ] ) ) ;
80
- return value ;
80
+ const formattedValue = formatResistorValue ( value ) ;
81
+ return formattedValue ;
81
82
} ;
82
83
84
+ function formatResistorValue ( resistance ) {
85
+ const prefixes = [ "" , "k" , "M" , "G" , "T" , "P" , "E" ] ;
86
+ let prefixIndex = 0 ;
87
+ while ( resistance >= 1000 && prefixIndex < prefixes . length - 1 ) {
88
+ resistance /= 1000 ;
89
+ prefixIndex ++ ;
90
+ }
91
+ return `${ resistance . toFixed ( 2 ) } ${ prefixes [ prefixIndex ] } ` ;
92
+ }
93
+
83
94
const calculateTolerance = ( color ) => {
84
95
return toleranceToValue ( color ) ;
85
96
} ;
@@ -89,98 +100,139 @@ export default function Home() {
89
100
const [ band2Color , setBand2Color ] = useState ( "black" ) ;
90
101
const [ band3Color , setBand3Color ] = useState ( "black" ) ;
91
102
const [ toleranceColor , setToleranceColor ] = useState ( "brown" ) ;
103
+ const [ selectedColors , setSelectedColors ] = useState ( {
104
+ color1 : "#1f2937" ,
105
+ color2 : "#1f2937" ,
106
+ color3 : "#1f2937" ,
107
+ color4 : "#a78b00" ,
108
+ } ) ;
92
109
93
110
const resistance = calculateResistance ( [ band1Color , band2Color , band3Color ] ) ;
94
111
const tolerance = calculateTolerance ( toleranceColor ) ;
95
112
113
+ function handleColorChange ( event ) {
114
+ const value = event . target . value ;
115
+ const name = event . target . name ;
116
+ const color = colors . find ( ( c ) => c . value === value ) ;
117
+ setSelectedColors ( ( prevState ) => ( {
118
+ ...prevState ,
119
+ [ name ] : color . hexCode ,
120
+ } ) ) ;
121
+ }
122
+
96
123
return (
97
- < div className = "container mx-auto py-8 " >
124
+ < div className = "flex flex-col p-4 " >
98
125
< Head >
99
126
< title > Resistor Calculator</ title >
100
127
< link rel = "icon" href = "/favicon.ico" />
101
128
</ Head >
102
129
103
- < h1 className = "text-3xl font-bold mb-8" > Resistor Calculator</ h1 >
104
-
105
- < div className = "flex mb-4" >
106
- < div className = "w-1/4" >
130
+ < div className = "flex flex-col gap-2 py-2 w-fit" >
131
+ < div className = "flex text-3xl font-bold" > Resistor Calculator</ div >
132
+ </ div >
133
+ < div className = "flex flex-col md:flex-row gap-4 py-4 w-full border border-slate-100 rounded shadow p-2 my-2" >
134
+ < div className = "md:w-1/4" >
107
135
< label className = "block text-gray-700 font-bold mb-2" >
108
136
Band 1 Color
109
137
</ label >
110
138
< select
139
+ name = "color1"
111
140
value = { band1Color }
112
- onChange = { ( e ) => setBand1Color ( e . target . value ) }
113
- className = "block appearance-none w-full bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded shadow leading-tight focus:outline-none focus:shadow-outline"
141
+ onChange = { ( e ) => {
142
+ setBand1Color ( e . target . value ) ;
143
+ handleColorChange ( e ) ;
144
+ } }
145
+ className = "block appearance-none w-full bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded-t shadow leading-tight focus:outline-none focus:shadow-outline border-b-0"
114
146
>
115
147
{ colors . map ( ( color ) => (
116
148
< option key = { color . value } value = { color . value } >
117
149
{ color . label }
118
150
</ option >
119
151
) ) }
120
152
</ select >
153
+ < div
154
+ className = "h-1 my-0 rounded-b"
155
+ style = { { backgroundColor : selectedColors . color1 } }
156
+ > </ div >
121
157
</ div >
122
- < div className = "w-1/4" >
158
+ < div className = "md: w-1/4" >
123
159
< label className = "block text-gray-700 font-bold mb-2" >
124
160
Band 2 Color
125
161
</ label >
126
162
< select
163
+ name = "color2"
127
164
value = { band2Color }
128
- onChange = { ( e ) => setBand2Color ( e . target . value ) }
129
- className = "block appearance-none w-full bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded shadow leading-tight focus:outline-none focus:shadow-outline"
165
+ onChange = { ( e ) => {
166
+ setBand2Color ( e . target . value ) ;
167
+ handleColorChange ( e ) ;
168
+ } }
169
+ className = "block appearance-none w-full bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded-t shadow leading-tight focus:outline-none focus:shadow-outline border-b-0"
130
170
>
131
171
{ colors . map ( ( color ) => (
132
172
< option key = { color . value } value = { color . value } >
133
173
{ color . label }
134
174
</ option >
135
175
) ) }
136
176
</ select >
177
+ < div
178
+ className = "h-1 my-0 rounded-b"
179
+ style = { { backgroundColor : selectedColors . color2 } }
180
+ > </ div >
137
181
</ div >
138
- < div className = "w-1/4" >
182
+ < div className = "md: w-1/4" >
139
183
< label className = "block text-gray-700 font-bold mb-2" >
140
184
Band 3 Color
141
185
</ label >
142
186
< select
187
+ name = "color3"
143
188
value = { band3Color }
144
- onChange = { ( e ) => setBand3Color ( e . target . value ) }
145
- className = "block appearance-none w-full bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded shadow leading-tight focus:outline-none focus:shadow-outline"
189
+ onChange = { ( e ) => {
190
+ setBand3Color ( e . target . value ) ;
191
+ handleColorChange ( e ) ;
192
+ } }
193
+ className = "block appearance-none w-full bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded-t shadow leading-tight focus:outline-none focus:shadow-outline border-b-0"
146
194
>
147
195
{ colors . map ( ( color ) => (
148
196
< option key = { color . value } value = { color . value } >
149
197
{ color . label }
150
198
</ option >
151
199
) ) }
152
200
</ select >
201
+ < div
202
+ className = "h-1 my-0 rounded-b"
203
+ style = { { backgroundColor : selectedColors . color3 } }
204
+ > </ div >
153
205
</ div >
154
- < div className = "w-1/4" >
206
+ < div className = "md: w-1/4" >
155
207
< label className = "block text-gray-700 font-bold mb-2" >
156
208
Tolerance
157
209
</ label >
158
210
< select
211
+ name = "color4"
159
212
value = { toleranceColor }
160
- onChange = { ( e ) => setToleranceColor ( e . target . value ) }
161
- className = "block appearance-none w-full bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded shadow leading-tight focus:outline-none focus:shadow-outline"
213
+ onChange = { ( e ) => {
214
+ setToleranceColor ( e . target . value ) ;
215
+ handleColorChange ( e ) ;
216
+ } }
217
+ className = "block appearance-none w-full bg-white border border-gray-400 hover:border-gray-500 px-4 py-2 pr-8 rounded-t shadow leading-tight focus:outline-none focus:shadow-outline border-b-0"
162
218
>
163
219
{ toleranceColors . map ( ( color ) => (
164
220
< option key = { color . value } value = { color . value } >
165
221
{ color . label }
166
222
</ option >
167
223
) ) }
168
224
</ select >
225
+ < div
226
+ className = "h-1 my-0 rounded-b"
227
+ style = { { backgroundColor : selectedColors . color4 } }
228
+ > </ div >
169
229
</ div >
170
230
</ div >
171
231
172
- < div className = "flex items-center justify-center mb-8" >
173
- < div className = "bg-gray-400 h-2 w-48 mr-2" > </ div >
174
- < div className = { `bg-${ band1Color } h-2 w-16 mr-2` } > </ div >
175
- < div className = { `bg-${ band2Color } h-2 w-16 mr-2` } > </ div >
176
- < div className = { `bg-${ band3Color } h-2 w-16 mr-2` } > </ div >
177
- < div className = { `bg-${ toleranceColor } h-2 w-16` } > </ div >
178
- </ div >
179
-
180
- < div className = "flex justify-between" >
232
+ < div className = "flex justify-between border border-slate-100 rounded shadow p-2 my-2" >
181
233
< div className = "flex-1 pr-4" >
182
234
< div className = "text-gray-700 font-bold mb-2" > Resistance</ div >
183
- < div className = "text-3xl font-bold" > { resistance . toFixed ( 2 ) } Ω</ div >
235
+ < div className = "text-3xl font-bold" > { resistance } Ω</ div >
184
236
</ div >
185
237
< div className = "flex-1" >
186
238
< div className = "text-gray-700 font-bold mb-2" > Tolerance</ div >
0 commit comments