1

Looking for a tikz-pgf solution to generate shapes with linear gradient fills that have multiple stops. So far I am able to get a single "stripe" with the following code snippet

\begin{tikzpicture}
    \draw (0,0) rectangle (5,1);
    \fill [left color=black,right color=black,middle color=white,shading angle=80] (0,0) rectangle (5,1);
\end{tikzpicture}

which produces the following picture Bar with gradient fill produced with tikz

What I would like is an image with multiple gradient stops like the image below with the ability to vary the number of stops and their colors.

Multiple gradient stops

2

You can get shadings of this type with \pgfdeclarehorizontalshading.

\documentclass[tikz,border=3mm]{standalone}
\pgfdeclarehorizontalshading{bwbwb}{4cm}{color(0cm)=(black); 
color(2cm)=(black); 
color(2.5cm)=(white); color(3cm)=(black); color(3.5cm)=(white); 
color(4cm)=(black);color(6cm)=(black)}
\begin{document}
\begin{tikzpicture}
 \draw[shading=bwbwb,shading angle=-20]  (0,0) rectangle (5,1);
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
  • Thanks - that's perfect. – user2501235 May 13 at 15:07

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.