Skip to content

Commit 1f6a9aa

Browse files
authored
Merge pull request #26 from jx2014/main
add color band for tolerance
2 parents 9b3fa75 + a1a9746 commit 1f6a9aa

1 file changed

Lines changed: 55 additions & 20 deletions

File tree

LabelGenerator.py

Lines changed: 55 additions & 20 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"),
226-
HexColor("#964B00"),
227-
HexColor("#FF3030"),
228-
HexColor("#FFA500"),
229-
HexColor("#FFFF00"),
230-
HexColor("#00FF00"),
231-
HexColor("#0000FF"),
232-
HexColor("#C520F6"),
233-
HexColor("#808080"),
234-
HexColor("#FFFFFF"),
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,
@@ -315,7 +329,8 @@ def draw_resistor_colorcode(
315329
y: float,
316330
width: float,
317331
height: float,
318-
num_codes: int
332+
num_codes: int,
333+
tolerance_value: Optional[float],
319334
) -> None:
320335

321336
if value.ohms_exp < num_codes - 4:
@@ -364,7 +379,7 @@ def draw_resistor_colorcode(
364379
y + border,
365380
stripe_width,
366381
height - 2 * border,
367-
-3)
382+
tolerance_color_table(tolerance_value))
368383

369384
c.setFillColor(black)
370385
c.setStrokeColor(black, 1)
@@ -480,7 +495,8 @@ def draw_resistor_sticker(
480495
column: int,
481496
ohms: float,
482497
draw_center_line: bool,
483-
mirror: bool
498+
mirror: bool,
499+
tolerance: Optional[float],
484500
) -> None:
485501
with StickerRect(c, layout, row, column, mirror) as rect:
486502

@@ -525,14 +541,14 @@ def draw_resistor_sticker(
525541
rect.left + rect.width/2,
526542
rect.bottom + rect.height/4 - rect.height/45,
527543
rect.width/4, rect.height/4,
528-
3)
544+
3, tolerance)
529545

530546
draw_resistor_colorcode(c, resistor_value,
531547
toColor("hsl(197, 59%, 100%)"), toColor("hsl(197, 59%, 73%)"),
532548
rect.left + rect.width * 0.75,
533549
rect.bottom + rect.height/4 - rect.height/45,
534550
rect.width/4, rect.height/4,
535-
4)
551+
4, tolerance)
536552

537553
c.setFont('Arial Bold', smd_font_size * 1.35)
538554
c.drawString(rect.left + rect.width/2 + rect.width/32, rect.bottom +
@@ -557,9 +573,10 @@ def render_stickers(
557573
c: Canvas,
558574
layout: PaperConfig,
559575
values: ResistorList,
576+
tolerance: Optional[float],
560577
draw_outlines: bool,
561578
draw_center_line: bool,
562-
draw_both_sides: bool
579+
draw_both_sides: bool,
563580
) -> None:
564581
def flatten(elem: Union[Optional[float], List[Optional[float]]]) -> List[Optional[float]]:
565582
if isinstance(elem, list):
@@ -586,9 +603,9 @@ def flatten(elem: Union[Optional[float], List[Optional[float]]]) -> List[Optiona
586603
begin_page(c, layout, draw_outlines)
587604

588605
if value is not None:
589-
draw_resistor_sticker(c, layout, rowId, columnId, value, draw_center_line, False)
606+
draw_resistor_sticker(c, layout, rowId, columnId, value, draw_center_line, False, tolerance)
590607
if draw_both_sides:
591-
draw_resistor_sticker(c, layout, rowId, columnId, value, False, True)
608+
draw_resistor_sticker(c, layout, rowId, columnId, value, False, True, tolerance)
592609

593610
# End the page one final time
594611
end_page(c)
@@ -689,6 +706,23 @@ def main() -> None:
689706
# ############################################################################
690707
# resistor_values: ResistorList = [ 0, 0.01 ] + generate_values(E24_COMMON_VALUES, 0, 6)
691708

709+
# ############################################################################
710+
# Resistor tolerance.
711+
#
712+
# Enable this if you want a specific tolerance value to be shown on
713+
# the resistors.
714+
#
715+
# 1: 1% brown
716+
# 2: 2% red
717+
# 0.5: 0.5% green
718+
# 0.25: 0.25% blue
719+
# 0.1: 0.1% violet
720+
# 5: 5% gold
721+
# 10: 10% silver
722+
# ############################################################################
723+
tolerance_value = None
724+
# tolerance_value = 1 # in percentage
725+
692726
# ############################################################################
693727
# Further configuration options
694728
#
@@ -719,7 +753,8 @@ def main() -> None:
719753
c = Canvas("ResistorLabels.pdf", pagesize=layout.pagesize)
720754

721755
# Render the stickers
722-
render_stickers(c, layout, resistor_values, draw_outlines, draw_center_line, draw_both_sides)
756+
render_stickers(c, layout, resistor_values, tolerance_value,
757+
draw_outlines, draw_center_line, draw_both_sides)
723758

724759
# Store canvas to PDF file
725760
c.save()

0 commit comments

Comments
 (0)