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
@@ -884,32 +888,48 @@ def _build_highlight(self, ref: str, info: PlacedComponentInfo,
884
888
self ._plotter .append_highlight_element (h )
885
889
886
890
def _apply_resistor_code (self , root : etree .Element , id_prefix : str , ref : str , value : str , properties : Dict [str , str ]) -> None :
887
- if root .find (f".//*[@id='{ id_prefix } res_4band1 ']" ) is None :
891
+ if root .find (f".//*[@id='{ id_prefix } res_band1 ']" ) is None :
888
892
return
889
893
try :
890
894
res , tolerance = self ._get_resistance_from_value (value , properties )
895
+ if res == 0 : # if exactly 0, un-hide the zero band mark
896
+ band = root .find (f".//*[@id='{ id_prefix } res_zeroband']" )
897
+ s = band .attrib ["style" ].split (";" )
898
+ for i in range (len (s )):
899
+ if s [i ].startswith ('display:' ):
900
+ s_split = s [i ].split (':' )
901
+ s_split [1 ] = 'inline'
902
+ s [i ] = ':' .join (s_split )
903
+ band .attrib ["style" ] = ";" .join (s )
904
+ return
891
905
if res < 0.001 :
892
- raise UserWarning ("resistance too small to represent" )
906
+ raise UserWarning (f "resistance too small to represent ( { res } ) " )
893
907
908
+ print (ref , res , tolerance )
909
+
910
+ # if more than 2%, then a 4 color band. otherwise a 5 color band
911
+ res_orig = res
894
912
if float (tolerance [:- 1 ]) > 2 :
895
913
power = math .floor (res .log10 ())
896
- if res >= 10 :
897
- power -= 1
898
914
res = Decimal (int (float (res ) / 10 ** power ))
915
+ if res < 10 :
916
+ power -= 1
917
+ res = Decimal (int (float (res_orig ) / 10 ** power ))
899
918
res = "{:02f}" .format (res )
900
919
resistor_colors = [
901
920
self ._plotter .get_style ("tht-resistor-band-colors" , int (str (res )[0 ])),
902
921
self ._plotter .get_style ("tht-resistor-band-colors" , int (str (res )[1 ])),
903
922
self ._plotter .get_style ("tht-resistor-band-colors" , int (power ))
904
923
]
905
- r_band = '4'
924
+ r_band = '' # the svg doesn't have a prefix to keep backwards compatibility
906
925
else :
907
926
power = math .floor (res .log10 ())
908
- if res >= 100 :
927
+ res = Decimal (int (float (res ) / 10 ** power ))
928
+ if res < 10 :
909
929
power -= 2
910
- elif res >= 10 :
930
+ elif res < 100 :
911
931
power -= 1
912
- res = Decimal ( int (float (res ) / 10 ** power ))
932
+ res = int (float (res_orig / Decimal ( 10 ** power ) ))
913
933
res = "{:03f}" .format (res )
914
934
resistor_colors = [
915
935
self ._plotter .get_style ("tht-resistor-band-colors" , int (str (res )[0 ])),
@@ -919,6 +939,9 @@ def _apply_resistor_code(self, root: etree.Element, id_prefix: str, ref: str, va
919
939
]
920
940
r_band = '5'
921
941
942
+ # safety check, to ensure the new resistor times power is the same as a truncated version of the original
943
+ assert math .isclose (float (res_orig ), float (res )* 10 ** power , rel_tol = 10 ** power )
944
+
922
945
if tolerance != '20%' :
923
946
resistor_colors += [self ._plotter .get_style ("tht-resistor-band-colors" , tolerance )]
924
947
@@ -946,6 +969,7 @@ def _apply_resistor_code(self, root: etree.Element, id_prefix: str, ref: str, va
946
969
def _get_resistance_from_value (self , value : str , properties : Dict [str , str ]) -> Tuple [Decimal , str ]:
947
970
res , tolerance = None , "5%"
948
971
value_l = value .split (" " , maxsplit = 1 )
972
+ print (value )
949
973
try :
950
974
res = read_resistance (value_l [0 ])
951
975
except ValueError :
@@ -972,7 +996,7 @@ def _get_resistance_from_value(self, value: str, properties: Dict[str, str]) ->
972
996
if not isinstance (s , dict ):
973
997
raise RuntimeError (f"Invalid style specified, tht-resistor-band-colors should be dictionary, got { type (s )} " )
974
998
if t_string .strip () not in s :
975
- raise UserWarning (f"Invalid resistor tolerance { value_l [ 1 ] } " )
999
+ raise UserWarning (f"Tolerance does not exist in style: { t_string } " )
976
1000
tolerance = t_string
977
1001
978
1002
return res , tolerance
0 commit comments