[Tex/LaTex] TikZ Fading Speed

tikz-pgf

In the fading example at
TikZ examples

I would like to change the "speed" at which fade occurs. In fact I would like it to be very "sharp" so that in the square examples you basically will see about half a square because it fades out so quick.

Any ideas how to achieve that? (basically I want to overlay two squares with different colors and end up seeing half a square of one color and the other half the other color but with a sort of quick fade between the two in the center(instead of the gradual fade that the default tikz setup gives)

Best Answer

By default, there's no option for setting the transition speed between the colors of a linear shading. You can relatively easily add it, though. Here's a new style fading speed=<0..100> that allow setting the transition speed for the standard axis shading.

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\begin{document}


\makeatletter
\tikzset{
    fading speed/.code={
        \pgfmathtruncatemacro\tikz@startshading{50-(100-#1)*0.25}
        \pgfmathtruncatemacro\tikz@endshading{50+(100-#1)*0.25}
        \pgfdeclareverticalshading[%
            tikz@axis@top,tikz@axis@middle,tikz@axis@bottom%
        ]{axis#1}{100bp}{%
            color(0bp)=(tikz@axis@bottom);
            color(\tikz@startshading)=(tikz@axis@bottom);
            color(50bp)=(tikz@axis@middle);
            color(\tikz@endshading)=(tikz@axis@top);
            color(100bp)=(tikz@axis@top)
        }
        \tikzset{shading=axis#1}
    }
}
\makeatother

\begin{tikzpicture}[top color=blue, bottom color=yellow]

\foreach \speed [count=\count] in {0,25,...,100}{
    \fill [fading speed=\speed] (\count,0) rectangle +(1,2);
    \path (\count,0) +(0.5,2) node [text=white,anchor=north] {\speed};
}
\end{tikzpicture}
\end{document}