Skip to content

Commit a1a9746

Browse files
committed
Refactor resistor tolerance values
1 parent 773d826 commit a1a9746

2 files changed

Lines changed: 50 additions & 44 deletions

File tree

LabelGenerator.py

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import math
1111
import sys
1212

13-
from typing import Tuple, Union, Optional, List
13+
from typing import Tuple, Union, Optional, List, Mapping
1414

1515
ResistorList = List[Union[Optional[float], List[Optional[float]]]]
1616

@@ -222,19 +222,33 @@ def format_value(self) -> str:
222222

223223
def resistor_color_table(num: int) -> HexColor:
224224
return [
225-
HexColor("#000000"), # black
226-
HexColor("#964B00"), # brown
227-
HexColor("#FF3030"), # red
228-
HexColor("#FFA500"), # orange
229-
HexColor("#FFFF00"), # yellow
230-
HexColor("#00FF00"), # green
231-
HexColor("#0000FF"), # blue
232-
HexColor("#C520F6"), # purple
233-
HexColor("#808080"), # grey
234-
HexColor("#FFFFFF"), # white
225+
HexColor("#000000"), # black
226+
HexColor("#964B00"), # brown
227+
HexColor("#FF3030"), # red
228+
HexColor("#FFA500"), # orange
229+
HexColor("#FFFF00"), # yellow
230+
HexColor("#00FF00"), # green
231+
HexColor("#0000FF"), # blue
232+
HexColor("#C520F6"), # violet
233+
HexColor("#808080"), # grey
234+
HexColor("#FFFFFF"), # white
235235
][num]
236236

237237

238+
def tolerance_color_table(tolerance_value: Optional[float]) -> int:
239+
tolerance_colors: Mapping[Optional[float], int] = {
240+
1: 1, # brown
241+
2: 2, # red
242+
0.5: 5, # green
243+
0.25: 6, # blue
244+
0.1: 7, # violet
245+
5: -1, # gold
246+
10: -2, # silver
247+
}
248+
249+
return tolerance_colors.get(tolerance_value, -3)
250+
251+
238252
def draw_fancy_resistor_stripe(
239253
c: Canvas,
240254
x: float,
@@ -264,7 +278,7 @@ def draw_resistor_stripe_border(c: Canvas, x: float, y: float, width: float, hei
264278
c.rect(x, y, width, height, fill=0, stroke=1)
265279

266280

267-
def draw_resistor_stripe(c: Canvas, x: float, y: float, width: float, height: float, stripe_value: int, tolerance_color = "grey") -> None:
281+
def draw_resistor_stripe(c: Canvas, x: float, y: float, width: float, height: float, stripe_value: int) -> None:
268282
if 0 <= stripe_value <= 9:
269283
c.setFillColor(resistor_color_table(stripe_value))
270284
c.rect(x, y, width, height, fill=1, stroke=0)
@@ -298,12 +312,11 @@ def draw_resistor_stripe(c: Canvas, x: float, y: float, width: float, height: fl
298312
else:
299313

300314
c.setLineWidth(0.5)
301-
#c.setFillColor(tolerance_color, 0.3)
302-
c.setFillColor(tolerance_color, 1)
315+
c.setFillColor(gray, 0.3)
303316
c.setStrokeColorRGB(0.5, 0.5, 0.5, 1.0)
304-
c.rect(x, y, width, height, fill=1, stroke=0)
305-
#c.line(x, y, x + width, y + height)
306-
#c.line(x + width, y, x, y + height)
317+
c.rect(x, y, width, height, fill=1, stroke=1)
318+
c.line(x, y, x + width, y + height)
319+
c.line(x + width, y, x, y + height)
307320
return
308321

309322

@@ -317,19 +330,9 @@ def draw_resistor_colorcode(
317330
width: float,
318331
height: float,
319332
num_codes: int,
320-
tolerance_value: int,
333+
tolerance_value: Optional[float],
321334
) -> None:
322335

323-
tolerance_color_table = {1: HexColor("#964B00"), # "brown"
324-
2: HexColor("#FF3030"), # "red",
325-
0.5: HexColor("#00FF00"), # "green"
326-
0.25: HexColor("#0000FF"), # "blue",
327-
0.1: "violet",
328-
5: "gold",
329-
10: "silver"}
330-
tolerance_color = tolerance_color_table.get(tolerance_value, "grey")
331-
332-
333336
if value.ohms_exp < num_codes - 4:
334337
return
335338

@@ -352,7 +355,7 @@ def draw_resistor_colorcode(
352355
y + border,
353356
stripe_width,
354357
height - 2 * border,
355-
0, tolerance_color)
358+
0)
356359
else:
357360
for i in range(num_codes):
358361

@@ -369,14 +372,14 @@ def draw_resistor_colorcode(
369372
y + border,
370373
stripe_width,
371374
height - 2 * border,
372-
stripe_value, tolerance_color)
375+
stripe_value)
373376

374377
draw_resistor_stripe(c,
375378
x + width - border - corner - stripe_width * 1.5,
376379
y + border,
377380
stripe_width,
378381
height - 2 * border,
379-
-3, tolerance_color)
382+
tolerance_color_table(tolerance_value))
380383

381384
c.setFillColor(black)
382385
c.setStrokeColor(black, 1)
@@ -493,7 +496,7 @@ def draw_resistor_sticker(
493496
ohms: float,
494497
draw_center_line: bool,
495498
mirror: bool,
496-
tolerance: int,
499+
tolerance: Optional[float],
497500
) -> None:
498501
with StickerRect(c, layout, row, column, mirror) as rect:
499502

@@ -570,10 +573,10 @@ def render_stickers(
570573
c: Canvas,
571574
layout: PaperConfig,
572575
values: ResistorList,
576+
tolerance: Optional[float],
573577
draw_outlines: bool,
574578
draw_center_line: bool,
575579
draw_both_sides: bool,
576-
tolerance: int
577580
) -> None:
578581
def flatten(elem: Union[Optional[float], List[Optional[float]]]) -> List[Optional[float]]:
579582
if isinstance(elem, list):
@@ -697,9 +700,18 @@ def main() -> None:
697700
[9100000000, 9200000000, 3300000000],
698701
]
699702

703+
# ############################################################################
704+
# Alternatively, a set of common resistor values can be generated by the
705+
# generate_values function.
706+
# ############################################################################
707+
# resistor_values: ResistorList = [ 0, 0.01 ] + generate_values(E24_COMMON_VALUES, 0, 6)
700708

701709
# ############################################################################
702-
# Resistor tolerance
710+
# Resistor tolerance.
711+
#
712+
# Enable this if you want a specific tolerance value to be shown on
713+
# the resistors.
714+
#
703715
# 1: 1% brown
704716
# 2: 2% red
705717
# 0.5: 0.5% green
@@ -708,14 +720,8 @@ def main() -> None:
708720
# 5: 5% gold
709721
# 10: 10% silver
710722
# ############################################################################
711-
712-
resistor_tolerance = 1 # in percentage
713-
714-
# ############################################################################
715-
# Alternatively, a set of common resistor values can be generated by the
716-
# generate_values function.
717-
# ############################################################################
718-
# resistor_values: ResistorList = [ 0, 0.01 ] + generate_values(E24_COMMON_VALUES, 0, 6)
723+
tolerance_value = None
724+
# tolerance_value = 1 # in percentage
719725

720726
# ############################################################################
721727
# Further configuration options
@@ -747,7 +753,8 @@ def main() -> None:
747753
c = Canvas("ResistorLabels.pdf", pagesize=layout.pagesize)
748754

749755
# Render the stickers
750-
render_stickers(c, layout, resistor_values, draw_outlines, draw_center_line, draw_both_sides, resistor_tolerance)
756+
render_stickers(c, layout, resistor_values, tolerance_value,
757+
draw_outlines, draw_center_line, draw_both_sides)
751758

752759
# Store canvas to PDF file
753760
c.save()

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ The generated labels include:
1010
- 4- and 5-band color codes
1111
- 3- and 4-digit smd codes
1212
- EIA-96 smd code
13-
- Tolerance color band
1413

1514
<img src="Example.svg">
1615

0 commit comments

Comments
 (0)