3

I wanted to depict a point moving over the unit circle using a rainbow circle. I managed to do that using the colour wheel. However, the colour wheel goes clockwise, and I need it to go counterclockwise, so I need to mirror it somehow.

I tried a negative scaling factor, but that didn't work because shading is applied after any transformation like this. Is there any way by which I can invert the colour wheel.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadings}
\begin{document}
\begin{tikzpicture}
\draw[->, ultra thick] (-2.7,0) -- (2.7,0);
\draw[->, ultra thick] (0,-2.7) -- (0,2.7);
\begin{scope}[xscale=-1]
    \shade[shading=color wheel,shading angle=-45,shift={(1.775,1.775)}] [even odd rule]
        (0,0) arc (45:405:2.5) (-0.03,-0.03) arc (45:405:2.45);
\end{scope}
\draw[, ultra thick] (-1.8,-1.8) -- (1.8,1.8);
\end{tikzpicture}
\end{document}

which produces:

enter image description here

  • In principle, you could take one of these wheels. However, at least I fail to exactly reproduce the output there, but perhaps you are more fortunate. – marmot Mar 27 '18 at 21:07
1

Please forget about my previous answer. All you need to do is to issue a `transform canvas.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadings}
\begin{document}
\begin{tikzpicture}
\draw[->, ultra thick] (-2.7,0) -- (2.7,0);
\draw[->, ultra thick] (0,-2.7) -- (0,2.7);
\begin{scope}[transform canvas={xscale=-1}]
    \shade[shading=color wheel,shading angle=-45,shift={(1.775,1.775)}] [even odd rule]
        (0,0) arc (45:405:2.5) (-0.03,-0.03) arc (45:405:2.45);
\end{scope}
\draw[, ultra thick] (-1.8,-1.8) -- (1.8,1.8);
\end{tikzpicture}
\end{document}

enter image description here

I really did not appreciate the transform canvas until I came across this post.

Historical artifacts: This is surprisingly tricky and the following is certainly not my answer, but just stolen from this answer. It turns out that there are some subtleties I was not aware of. The shading seems to call some low-level postscript commands that I fail to understand. Even worse, the outcome depends on the viewer. This is in particular true for the code that follows.

\documentclass[tikz,border=5]{standalone}

\makeatletter
\newtoks\pgf@ps@toks
\newcount\c@pgf@ps

\def\pgf@ps@sp{ }

\def\pgf@ps@esettoks#1{\edef\pgf@ps@tmp{#1}\pgf@ps@toks\expandafter{\pgf@ps@tmp}}

\def\pgf@ps@repop#1#2{%
  \c@pgf@countb=#2\relax%
  \def\pgf@ps@op{#1}%
  \def\pgf@ps@ops{}\pgf@ps@@repop}
\def\pgf@ps@@repop{%
  \ifnum\c@pgf@countb<1\relax%
  \else%
    \edef\pgf@ps@ops{\pgf@ps@op\pgf@ps@ops}%
    \advance\c@pgf@countb by-1\relax%
    \expandafter\pgf@ps@@repop%
  \fi%
}

\def\pgf@ps@generate@ps{%
  \c@pgf@counta=\pgf@ps@ncol\relax%
  \c@pgf@countb=\c@pgf@counta%
  \advance\c@pgf@countb by-1\relax%
  \pgf@ps@esettoks{ \noexpand\pgf@ps@interp{col@\the\c@pgf@counta}{col@\the\c@pgf@countb} }%
  \pgfmathloop
  \ifnum\c@pgf@counta<2\relax%
  \else%
    \c@pgf@countb=-\c@pgf@counta%
    \advance\c@pgf@countb by\pgf@ps@ncol\relax%
    \advance\c@pgf@counta by-1\relax%
    \pgf@ps@repop{pop\pgf@ps@sp}{\c@pgf@countb}%
    \c@pgf@countb=\c@pgf@counta%
    \advance\c@pgf@countb by-1\relax%
    \ifnum\c@pgf@countb=0\relax%
      \c@pgf@countb=\pgf@ps@ncol\relax%
    \fi%
    \pgf@ps@esettoks{ \the\c@pgf@counta\pgf@ps@sp eq { \pgf@ps@ops \noexpand\pgf@ps@interp{col@\the\c@pgf@counta}{col@\the\c@pgf@countb} }{ \the\pgf@ps@toks } ifelse}%
  \repeatpgfmathloop%
  \c@pgf@counta=\pgf@ps@ncol\relax%
  \advance\c@pgf@counta by-2\relax%
  \pgf@ps@repop{dup\pgf@ps@sp}{\c@pgf@counta}%
  \pgf@ps@esettoks{ \pgf@ps@ops \the\pgf@ps@toks }%
}

\def\pgf@ps@colorstorgb#1{%
  \c@pgf@ps=1\relax%
  \pgfutil@for\pgf@ps@:={#1}\do{%
     \pgf@ps@coltorgb{\pgf@ps@}{col@\the\c@pgf@ps}%
     \advance\c@pgf@ps by1\relax}%
}

\def\pgf@ps@coltorgb#1#2{%
 \edef\pgf@ps@marshal{\noexpand\pgfshadecolortorgb{#1}}%
 \expandafter\pgf@ps@marshal\expandafter{\csname#2\endcsname}%
}

\def\pgf@ps@rgb#1{\csname#1\endcsname}
\def\pgf@ps@interp#1#2{%
  \pgf@ps@rgb{#1red} mul exch \pgf@ps@rgb{#2red} mul add
    5 1 roll
  \pgf@ps@rgb{#1green} mul exch \pgf@ps@rgb{#2green} mul add
    3 1 roll
  \pgf@ps@rgb{#1blue} mul exch \pgf@ps@rgb{#2blue} mul add
}




\def\pgfdeclarecolorwheelshading#1#2#3{%
  \pgf@ps@getcols{#3}%
  \pgfmathparse{mod(#2+360/\pgf@ps@ncol-90,360)}%
  \pgf@x=\pgfmathresult pt\relax%
  \ifdim\pgf@x<0pt\relax%
    \advance\pgf@x by360pt\relax%
  \fi%
  \edef\pgf@ps@rot{\pgfmath@tonumber{\pgf@x}}%
  \pgf@ps@generate@ps%
  \pgf@ps@esettoks{%
    \noexpand\pgfdeclarefunctionalshading[#3]{#1}%
    {\noexpand\pgfpoint{-50bp}{-50bp}}{\noexpand\pgfpoint{50bp}{50bp}}%
    {\noexpand\pgf@ps@colorstorgb{#3}}%
    {%
      2 copy abs exch abs add 0.0001 ge { atan } { pop } ifelse
      \pgf@ps@rot\pgf@ps@sp add dup 360 ge { -360 add } { } ifelse
      360 \pgf@ps@ncol\pgf@ps@sp 
      div div dup floor dup 3 1 roll neg add dup neg 1 add exch
      2 copy 2 copy 7 -1 roll 1 add
      \the\pgf@ps@toks}}%
  \edef\pgf@ps@marshal{\the\pgf@ps@toks}%
  \pgf@ps@marshal}

\def\pgf@ps@getcols#1{%
  \c@pgf@ps=0\relax%
  \pgfutil@for\pgf@ps@:={#1}\do{\advance\c@pgf@ps by1}%
  \edef\pgf@ps@ncol{\the\c@pgf@ps}%
}

\pgfdeclarecolorwheelshading{rgb}{0}{red,blue,green}
\pgfdeclarecolorwheelshading{rgb-flipped}{90}{red,blue,green}
\pgfdeclarecolorwheelshading{rainbow}{-45}{red,yellow,pink,green,orange,purple,blue}
\begin{document}

\begin{tikzpicture}
\draw[->, ultra thick] (-2.7,0) -- (2.7,0);
\draw[->, ultra thick] (0,-2.7) -- (0,2.7);
\begin{scope}
    \shade[shading=rgb-flipped,rotate=45] [even odd rule]
        (0,0) circle (2.5) (0,0) circle (2.4);
\end{scope}
\draw[, ultra thick] (-1.8,-1.8) -- (1.8,1.8);
\end{tikzpicture}
\end{document}

enter image description here

This screenshot is made with acroread, with which I can confirm the screenshots from this answer, but for instance with preview I can't. What is, however, surprising to me is that the original color wheel looks the same in both viewers. Note also that the code in pgflibraryshadings.code.tex looks quite different from the one above. One possible interpretation is that the code pgflibraryshadings.code.tex is more failsafe in the sense that the outcome won't depend that much the viewer. but these are only my speculations. If you know postscript well enough, you may just change the code there. I played with it but despite some partial successes I never arrived at something really useful. I hope you'll get a better answer than this one.

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.