0

I am trying to draw a path with a double color gradient, first magenta to cyan then cyan to magenta.

I have tried to use the code from this thread, but the best I could do is this:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{decorations.markings}


\tikzset{multicolor/.style={
        postaction={
            decorate,
            decoration={
                markings,
                mark=between positions 0 and 0.5*\pgfdecoratedpathlength step 0.5pt with {
                    \pgfmathsetmacro\myval{multiply(divide(
                        \pgfkeysvalueof{/pgf/decoration/mark info/distance from start}, 1*\pgfdecoratedpathlength),200)};
                    \pgfsetfillcolor{cyan!\myval!magenta};
                    \pgfpathcircle{\pgfpointorigin}{#1};
                    \pgfusepath{fill};},
                mark=between positions 0.5*\pgfdecoratedpathlength and \pgfdecoratedpathlength step 0.5pt with {
                    \pgfmathsetmacro\myval{300+multiply(divide(
                        \pgfkeysvalueof{/pgf/decoration/mark info/distance from start}, 1*\pgfdecoratedpathlength),200)};
                    \pgfsetfillcolor{magenta!\myval!cyan};
                    \pgfpathcircle{\pgfpointorigin}{#1};
                    \pgfusepath{fill};},
}}}}


\begin{document}
    
    \begin{tikzpicture}
        \path [multicolor=0.3mm,draw] (0,0) .. controls (1,3) and (5,-7) .. (4,1)  {};
    \end{tikzpicture}
    
    
\end{document}

which produces this: enter image description here

I would like the second part of the path to gradually change from cyan to magenta.

Thanks in advance

1 Answer 1

1

Just an adjustment to the formula.

I've also added overlay to the path itself so that the bounding box does not include the control points but \pgf@relevantforpicturesizetrue (i.e. overlay = false) so that the circles themselves are counted towards the bounding box of the picture.

The path itself is not drawn and the multicolor style does not include postaction anymore because the line would be hidden by the circles anyway.

Code

\documentclass[tikz]{standalone}
%\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\tikzset{
  multicolor/.style={
    decorate,
    decoration={
      markings,
      mark=between positions 0 and 0.5*\pgfdecoratedpathlength step 0.5pt with {
        \csname pgf@relevantforpicturesizetrue\endcsname % overlay = false
        \pgfmathparse{multiply(divide(
          \pgfkeysvalueof{/pgf/decoration/mark info/distance from start},
          \pgfdecoratedpathlength),200)}
        \pgfsetfillcolor{cyan!\pgfmathresult!magenta}
        \pgfpathcircle{\pgfpointorigin}{#1}
        \pgfusepath{fill}},
      mark=between positions 0.5*\pgfdecoratedpathlength and
        \pgfdecoratedpathlength step 0.5pt with {
        \csname pgf@relevantforpicturesizetrue\endcsname % overlay = false
        \pgfmathparse{subtract(multiply(divide(
          \pgfkeysvalueof{/pgf/decoration/mark info/distance from start},
          \pgfdecoratedpathlength),200),100)}
        \pgfsetfillcolor{magenta!\pgfmathresult!cyan}
        \pgfpathcircle{\pgfpointorigin}{#1}
        \pgfusepath{fill}}}}}

\begin{document}
\tikz % overlay because of control points
  \path[multicolor=0.3mm, overlay] (0,0) .. controls (1,3) and (5,-7) .. (4,1);
\end{document}

Output

enter image description here

1
  • amazing, thanks!!
    – Andros
    Nov 25, 2023 at 12:48

You must log in to answer this question.

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