57
57
"highlight-padding" : 1.5 ,
58
58
"highlight-offset" : 0 ,
59
59
"tht-resistor-band-colors" : {
60
+ - 3 : '#d8a0a6' ,
60
61
- 2 : '#d9d9d9' ,
61
62
- 1 : '#ffc800' ,
62
63
0 : '#000000' ,
69
70
7 : '#cc00cc' ,
70
71
8 : '#666666' ,
71
72
9 : '#cccccc' ,
73
+ # resistor tolerances
74
+ '10%' : '#d9d9d9' ,
75
+ '5%' : '#ffc800' ,
72
76
'1%' : '#805500' ,
73
77
'2%' : '#ff0000' ,
78
+ '0.05%' : '#ff8000' ,
79
+ '0.02%' : '#ffff00' ,
74
80
'0.5%' : '#00cc11' ,
75
81
'0.25%' : '#0000cc' ,
76
82
'0.1%' : '#cc00cc' ,
77
- '0.05%' : '#666666' ,
78
- '5%' : '#ffc800' ,
79
- '10%' : '#d9d9d9' ,
83
+ '0.01%' : '#666666' ,
80
84
}
81
85
}
82
86
@@ -886,32 +890,48 @@ def _build_highlight(self, ref: str, info: PlacedComponentInfo,
886
890
self ._plotter .append_highlight_element (h )
887
891
888
892
def _apply_resistor_code (self , root : etree .Element , id_prefix : str , ref : str , value : str , properties : Dict [str , str ]) -> None :
889
- if root .find (f".//*[@id='{ id_prefix } res_4band1 ']" ) is None :
893
+ if root .find (f".//*[@id='{ id_prefix } res_band1 ']" ) is None :
890
894
return
891
895
try :
892
896
res , tolerance = self ._get_resistance_from_value (value , properties )
897
+ if res == 0 : # if exactly 0, un-hide the zero band mark
898
+ band = root .find (f".//*[@id='{ id_prefix } res_zeroband']" )
899
+ s = band .attrib ["style" ].split (";" )
900
+ for i in range (len (s )):
901
+ if s [i ].startswith ('display:' ):
902
+ s_split = s [i ].split (':' )
903
+ s_split [1 ] = 'inline'
904
+ s [i ] = ':' .join (s_split )
905
+ band .attrib ["style" ] = ";" .join (s )
906
+ return
893
907
if res < 0.001 :
894
- raise UserWarning ("resistance too small to represent" )
908
+ raise UserWarning (f "resistance too small to represent ( { res } ) " )
895
909
910
+ print (ref , res , tolerance )
911
+
912
+ # if more than 2%, then a 4 color band. otherwise a 5 color band
913
+ res_orig = res
896
914
if float (tolerance [:- 1 ]) > 2 :
897
915
power = math .floor (res .log10 ())
898
- if res >= 10 :
899
- power -= 1
900
916
res = Decimal (int (float (res ) / 10 ** power ))
917
+ if res < 10 :
918
+ power -= 1
919
+ res = Decimal (int (float (res_orig ) / 10 ** power ))
901
920
res = "{:02f}" .format (res )
902
921
resistor_colors = [
903
922
self ._plotter .get_style ("tht-resistor-band-colors" , int (str (res )[0 ])),
904
923
self ._plotter .get_style ("tht-resistor-band-colors" , int (str (res )[1 ])),
905
924
self ._plotter .get_style ("tht-resistor-band-colors" , int (power ))
906
925
]
907
- r_band = '4'
926
+ r_band = '' # the svg doesn't have a prefix to keep backwards compatibility
908
927
else :
909
928
power = math .floor (res .log10 ())
910
- if res >= 100 :
929
+ res = Decimal (int (float (res ) / 10 ** power ))
930
+ if res < 10 :
911
931
power -= 2
912
- elif res >= 10 :
932
+ elif res < 100 :
913
933
power -= 1
914
- res = Decimal ( int (float (res ) / 10 ** power ))
934
+ res = int (float (res_orig / Decimal ( 10 ** power ) ))
915
935
res = "{:03f}" .format (res )
916
936
resistor_colors = [
917
937
self ._plotter .get_style ("tht-resistor-band-colors" , int (str (res )[0 ])),
@@ -921,6 +941,9 @@ def _apply_resistor_code(self, root: etree.Element, id_prefix: str, ref: str, va
921
941
]
922
942
r_band = '5'
923
943
944
+ # safety check, to ensure the new resistor times power is the same as a truncated version of the original
945
+ assert math .isclose (float (res_orig ), float (res )* 10 ** power , rel_tol = 10 ** power )
946
+
924
947
if tolerance != '20%' :
925
948
resistor_colors += [self ._plotter .get_style ("tht-resistor-band-colors" , tolerance )]
926
949
@@ -948,6 +971,7 @@ def _apply_resistor_code(self, root: etree.Element, id_prefix: str, ref: str, va
948
971
def _get_resistance_from_value (self , value : str , properties : Dict [str , str ]) -> Tuple [Decimal , str ]:
949
972
res , tolerance = None , "5%"
950
973
value_l = value .split (" " , maxsplit = 1 )
974
+ print (value )
951
975
try :
952
976
res = read_resistance (value_l [0 ])
953
977
except ValueError :
@@ -974,7 +998,7 @@ def _get_resistance_from_value(self, value: str, properties: Dict[str, str]) ->
974
998
if not isinstance (s , dict ):
975
999
raise RuntimeError (f"Invalid style specified, tht-resistor-band-colors should be dictionary, got { type (s )} " )
976
1000
if t_string .strip () not in s :
977
- raise UserWarning (f"Invalid resistor tolerance { value_l [ 1 ] } " )
1001
+ raise UserWarning (f"Tolerance does not exist in style: { t_string } " )
978
1002
tolerance = t_string
979
1003
980
1004
return res , tolerance
0 commit comments