1

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).

enter image description here

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}

2 Answers 2

2

Use an optional argument which can be easily extended to a flexible number of colors.

\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
\define@key[psset]{pstricks}{ResistorCol}{\pst@setRcolor#1,,,!!}
\def\pst@setRcolor#1,#2,#3,#4!!{%
  \def\psResistorColA{#1}\def\psResistorColB{#2}\def\psResistorColC{#3}}
\psset{ResistorCol={}}%   no color lines

\def\psResistor{\pst@object{psResistor}}
\def\psResistor@i{\@ifnextchar({\psResistor@ii}{\psResistor@ii(0,0)}}
\def\psResistor@ii(#1){%
  \begingroup
  \use@par
  \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)
    \ifx\psResistorColA\@empty\else
      \psline[linewidth=5pt,linecolor=\psResistorColA](-.7,-.5)(-.7,.5)
      \psline[linewidth=5pt,linecolor=\psResistorColB](-.3,-.5)(-.3,.5)
      \psline[linewidth=5pt,linecolor=\psResistorColC]( .1,-.5)( .1,.5)
      \psline[linewidth=5pt,linecolor=black]( .7,-.5)( .7,.5)
    \fi
  }% end rput.
  \endgroup
}% end psResistor@ii.
\makeatother

\begin{pspicture}(-1.5,-1)(3,2)
\psResistor(0,1.1)
\psResistor[ResistorCol={3,4,5}](0,0)
\end{pspicture}

\end{document}

enter image description here

0

In this answer I am going to elaborate on Herbert's solution using the pst-circ package. Going further I implement a solution that accepts different numbers of color bands (3-6) and it will also distribute the colors evenly: the color bands are at different positions depending on the overall number of bands, and the spacing changes.

\documentclass{standalone}
\usepackage{pst-circ}

% Definition of the colors
\definecolor{0}{RGB}{45 45 45}%
\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}{125 125 125}%
\definecolor{9}{named}{white}%
\definecolor{-2}{RGB}{200 200 200}%
\definecolor{-1}{RGB}{212 175 55}%

\makeatletter
\define@key[psset]{pstricks}{ResistorCol}{\pst@setRcolor#1,,,,,,!!}
\def\pst@setRcolor#1,#2,#3,#4,#5,#6,#7!!{% #7 will gobble all ',' !
  \def\psResistorColA{#1}% Band A
  \def\psResistorColB{#2}% Band B
  \def\psResistorColC{#3}% Band C
  \def\psResistorColD{#4}% Band D
  \def\psResistorColE{#5}% Band E
  \def\psResistorColF{#6}}%Band F and end \def setRcolor.

\newCircDipole{psResistor}%
\def\pst@draw@psResistor{%
\begingroup%
\use@par%
  % draw the resistor:
  \psclip{\pspolygon[%
  linearc=.1,linestyle=none,fillstyle=solid,fillcolor=cyan!15]% 
  (.5,.3)(.35,.3)(.31,.27)(.27,.24)% upper right corner.
  (-.27,.24)(-.31,.27)(-.35,.3)(-.5,.3)% upper left.
  (-.5,-.3)(-.35,-.3)(-.31,-.27)(-.27,-.24)% lower left.
  (.27,-.24)(.31,-.27)(.35,-.3)(.5,-.3)% lower right.
  }%
  \psset{dotstyle=B|,dotsize=.6\psyunit}%
  \ifx\psResistorColF\@empty
  \ifx\psResistorColE\@empty
  \ifx\psResistorColD\@empty
    % just three present:
    \psset{xunit=1.4\psxunit}
    \rput(-.1,0){
    \psdot[linecolor=\psResistorColA](-.08,0)
    \psdot[linecolor=\psResistorColB]( .07,0)
    \psdot[linecolor=\psResistorColC]( .27,0)}% end rput.
  \else% four present
    \psset{xunit=1.25\psxunit}
   % \rput(.1,0){
    \psdot[linecolor=\psResistorColA](-.23,0)
    \psdot[linecolor=\psResistorColB](-.08,0)
    \psdot[linecolor=\psResistorColC]( .07,0)
    \psdot[linecolor=\psResistorColD]( .27,0)%}% end rput.
    \fi
  \else% five present:
    \psset{xunit=1.1\psxunit}
    \rput(-.1,0){
    \psdot[linecolor=\psResistorColA](-.23,0)
    \psdot[linecolor=\psResistorColB](-.08,0)
    \psdot[linecolor=\psResistorColC]( .07,0)
    \psdot[linecolor=\psResistorColD]( .27,0)
    \psdot[linecolor=\psResistorColE]( .42,0)}% end rput.
    \fi
  \else% six present:
    \psdot[linecolor=\psResistorColA](-.38,0)
    \psdot[linecolor=\psResistorColB](-.23,0)
    \psdot[linecolor=\psResistorColC](-.08,0)
    \psdot[linecolor=\psResistorColD]( .07,0)
    \psdot[linecolor=\psResistorColE]( .27,0)
    \psdot[linecolor=\psResistorColF]( .42,0)
  \fi
  \endpsclip%
\endgroup%
  % Re-draw the line around the shape (copy paste):
  \pspolygon[linearc=.1,linewidth=0.7\pslinewidth]% 
  (.5,.3)(.35,.3)(.31,.27)(.27,.24)% upper right corner.
  (-.27,.24)(-.31,.27)(-.35,.3)(-.5,.3)% upper left.
  (-.5,-.3)(-.35,-.3)(-.31,-.27)(-.27,-.24)% lower left.
  (.27,-.24)(.31,-.27)(.35,-.3)(.5,-.3)% lower right.
  % node connections (c.f. pst-circ):
  \pnode(-.51,0){dipole@1}%
  \pnode(.51,0){dipole@2}%
}% end definition of \pst@draw@psResistor.
\makeatother

\begin{document}
\begin{pspicture}(-1,-1)(2,1)
    \psResistor[ResistorCol={4,5,6}](-1,0)(2,0){}
\end{pspicture}
\end{document}

I realize this code could easily be improved upon, please feel free to do so. Some examples using the code are the following:

\psResistor[ResistorCol={4,5,6}](-1,0)(2,0){}
\psResistor[ResistorCol={4,5,6,3}](-1,0)(2,0){}
\psResistor[ResistorCol={4,5,6,7,2}](-1,0)(2,0){}
\psResistor[ResistorCol={8,9,7,-1,2,0}](-1,0)(2,0){}

resistors one and two

resistors three and four

Please note that there is no implementation of the technical restrictions. For example the first band can be set to silver (-2), but this will not be a color encoding of a valid resistor.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .