# Generate new or change a line color with Postscript notation

I'd like to use

\psset{linecolor = blue!\mapDFVtoColor}


whereas \mapDFVtoColor is defined as something like

\def\mapDFVtoColor{20 40 add 3.5 div}


Here's a bit longer but condensed example

\documentclass[dvips]{standalone}

\begin{document}
\begin{pspicture}[showgrid = true](-4.5, -4.5)(4.5, 5)
\def\Bdeg{30\space}

\def\xp{2\space}
\def\yp{1\space}
\def\zp{1\space}

\def\DistanceFromViewer{%
\radius dup mul sub abs %

\def\MinDist{0\space}
\def\MinColor{40\space}
\def\MaxColor{100\space}

\def\mapDFVtoColor{%
\DistanceFromViewer \MinDist sub %
\MaxColor \MinColor sub mul %
\MaxDist \MinDist sub div %

\rput(1, 1){Distance: \psPrintValue{\DistanceFromViewer}}
\rput(1, 0){Map: \psPrintValue{\mapDFVtoColor}}

\psset{linecolor = blue!\mapDFVtoColor}

\end{pspicture}
\end{document}


The idea is that I want to scale the colour of a point in 3D with the distance to the observer. Thanks for comments.

• If I try seeing what's the value of \mapDFVtoColor at the point of usage, I see 4 30 cos 68 sin mul mul 2 mul 4 68 cos 30 cos mul mul 1 mul add 4 30 sin mul 1 mul add 4 dup mul sub abs 4 div 0 sub 100 40 sub mul 4 2 mul 0 sub div 40 add which clearly is not something good for the xcolor syntax blue!<number> you want to use. Those computations are performed only at the PostScript level, which happens during conversion DVI → PS, so they're not available during the TeX run. – egreg Mar 22 '15 at 11:47
• Yes ;) So I'm wondering how to convert that line into a number. – Clemens Mar 22 '15 at 11:49
• You have to use a number engine at the TeX level, say fp or pgfmath or expl3. – egreg Mar 22 '15 at 11:51
• Thanks a lot, using fp worked perfectly. Having the question solved, how can I upvote or mark this one as solved? Is it helpful to uploading the new code? – Clemens Mar 22 '15 at 12:18

Here's my answer with respect to egreg's comment:

\documentclass[dvips]{standalone}
\usepackage{fp}

\begin{document}
\begin{pspicture}[showgrid = true](-4.5, -4.5)(4.5, 5)
\def\Bdeg{30\space}

\def\xp{2\space}
\def\yp{1\space}
\def\zp{1\space}

\def\DistanceFromViewer{%
\radius dup mul sub abs %

\FPeval{\resultDistanceFromViewer}{%
abs(\radius * cos(\FPpi * \Bdeg / 180) * sin(\FPpi * \Adeg / 180) * \xp + %
\radius * cos(\FPpi * \Adeg / 180) * cos(\FPpi * \Bdeg / 180) * \yp + %
\radius * sin(\FPpi * \Bdeg / 180) * \zp - %

\def\MinDist{0\space}
\def\MinColor{40\space}
\def\MaxColor{100\space}

\def\mapDFVtoColor{%
\DistanceFromViewer \MinDist sub %
\MaxColor \MinColor sub mul %
\MaxDist \MinDist sub div %

\FPeval{\resulmapDFVtoColor}{%
(\resultDistanceFromViewer - \MinDist) * %
(\MaxColor - \MinColor) / (\MaxDist - \MinDist) + \MinColor}

\rput(1, 1){Distance: \psPrintValue{\DistanceFromViewer}}
\rput(1, 0){Map: \psPrintValue{\mapDFVtoColor}}
\rput(1, -1){Distance: \resultDistanceFromViewer}
\rput(1, -2){Map: \resulmapDFVtoColor}

\end{pspicture}
\end{document}


It shows also a comparison between the results you get from the postscript code and the fp number crunching. Calling \psset{linecolor = blue!\resulmapDFVtoColor} works with that as well.