1

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}
\usepackage{pstricks, pstricks-add}

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

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

    \def\DistanceFromViewer{%
    \radius \Bdeg cos \Adeg sin mul mul \xp mul %
    \radius \Adeg cos \Bdeg cos mul mul \yp mul add %
    \radius \Bdeg sin mul \zp mul add %
    \radius dup mul sub abs %
    \radius div\space}

    \def\MinDist{0\space}
    \def\MaxDist{\radius 2 mul\space}
    \def\MinColor{40\space}
    \def\MaxColor{100\space}

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

    \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
  • 1
    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
  • Add a self-answer – egreg Mar 22 '15 at 13:06
1

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

\documentclass[dvips]{standalone}
\usepackage{pstricks, pstricks-add}
\usepackage{fp}

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

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

    \def\DistanceFromViewer{%
    \radius \Bdeg cos \Adeg sin mul mul \xp mul %
    \radius \Adeg cos \Bdeg cos mul mul \yp mul add %
    \radius \Bdeg sin mul \zp mul add %
    \radius dup mul sub abs %
    \radius div\space}

    \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 - %
    \radius * \radius) / (\radius)}

    \def\MinDist{0\space}
    \def\MaxDist{\radius 2 mul\space}
    \def\MinColor{40\space}
    \def\MaxColor{100\space}

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

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.