13

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)

1
  • I'd try using a fading (Section 20.3 of the PGF2.10 manual). You can create one from an arbitrary picture to do almost whatever you like. Commented Mar 28, 2012 at 11:08

1 Answer 1

21

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}
4
  • 1
    cool, I didn't realize you could do these types of things.
    – Uiy
    Commented Mar 28, 2012 at 12:46
  • Would it be possible to modify this so it takes into account the shading angle and initial direction? I would attempt to modify it myself but I don't quite understand the text macro ;/ I'm having issues with it since it uses a fixed direction and my code uses multiple directions(shades based on an angle that changes)
    – Uiy
    Commented Mar 28, 2012 at 13:04
  • You can just set shading angle=<angle> to rotate the shading. Is that what you're looking for? Otherwise, it might be best to open a new question.
    – Jake
    Commented Mar 28, 2012 at 13:08
  • This doesn't give the same effect as fading angle. I am using various fading angles and I want shading to work. I'm thinking it might not be your code as it does seem to change angles but happens to be the way things work. I'll open a new question for what I really want to do instead of creating tricks.
    – Uiy
    Commented Mar 28, 2012 at 13:11

You must log in to answer this question.

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