My goal is to write a pstricks macro that will draw resistors with their corresponding color band, c.f. Wikipedia. My idea is to define a command psResistor that works like this one: \psResistor(0,0){3,4,5,6}
placing the resistor in the origin with colors 3 (orange), 4 (yellow), and 5 (green) and with a little wider distance 6 (blue).
So first I define the colors with \definecolor
. My question to this bit is where it will be best to put those definitions. I don't necessarily want them available elsewhere.
I then define a PSTricks object psResistor
that will handle optional arguments, and then passes the colors to \psResistor@iii
. Now that's the actual question I have: The number of arguments may differ. If it's three, I add one band in black. It may be four, it may be five, it may be six. What I need is some sort of clever mechanism that will distribute the colors accordingly.
I have encountered a similar problem in the past with the definition of \vector
, c.f. typesetting column vector
I used a solution that works with additional ,
but maybe there is a better way here, one that will also deal with the additional space to the last color band.
There is a similar question on color band resistors for Tikz, but not for automatic colors here: Draw resistor with band color code
Here is what I have so far:
\documentclass{standalone}
\usepackage{pstricks}
\begin{document}
\definecolor{0}{RGB}{50 50 50}
\definecolor{1}{named}{brown}
\definecolor{2}{named}{red}
\definecolor{3}{named}{orange}
\definecolor{4}{named}{yellow}
\definecolor{5}{named}{green}
\definecolor{6}{named}{blue}
\definecolor{7}{named}{violet}
\definecolor{8}{RGB}{115 115 115}
\definecolor{9}{named}{white}
\definecolor{-1}{RGB}{200 200 200}
\definecolor{-2}{RGB}{212 175 55}
\makeatletter
\def\psResistor{\pst@object{psResistor}}
\def\psResistor@i{\@ifnextchar({\psResistor@ii}{\psResistor@ii(0,0)}}
\def\psResistor@ii(#1)#2{%
\begin@ClosedObj%
\rput(#1){
\psline(-2,0)(-1,0)
\psframe[% draw frame of the resistor:
fillstyle=solid,fillcolor=cyan!15,dimen=inner](-1,-.5)(1,.5)
\psline(1,0)(2,0)
\psResistor@iii(#2)% add the colored lines
}% end rput.
\end@ClosedObj%
}% end psResistor@ii.
\def\psResistor@iii(#1,#2,#3){
\psline[linewidth=5pt,linecolor=#1](-.7,-.5)(-.7,.5)
\psline[linewidth=5pt,linecolor=#2](-.3,-.5)(-.3,.5)
\psline[linewidth=5pt,linecolor=#3]( .1,-.5)( .1,.5)
\psline[linewidth=5pt,linecolor=black]( .7,-.5)( .7,.5)
}% end \psResistor@iii.
\makeatother
\psset{unit=1cm}
\begin{pspicture}(-1.5,-1)(3,1)
\psResistor(0,0){3,4,5}
\end{pspicture}
\end{document}