8

In the following example:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,calc,shapes,shapes.geometric,patterns}
\begin{document}
\begin{tikzpicture}
\shade[bottom color=cyan!60!black, top color=red, middle color = blue!20!white] (0,0) rectangle (4,5);
\end{tikzpicture}
\end{document}

Is it possible to instead of having the 'middle color' being in the middle of the rectange but to shift it say up by a quarter so that the 'middle color' separates the 'top' and 'bottom' color at three quarters of the way up?

  • 2
    You can fake it using twice \shade[bottom color=cyan!60!black, top color=red, middle color = blue!20!white] (0,0) rectangle (4,5); and changing the coordinates to produce two shades one above the other. – Sigur Jul 16 '14 at 14:15
  • changing what coordinates? – KatyB Jul 16 '14 at 14:18
  • The rectangle coordinates. – Sigur Jul 16 '14 at 14:24
  • I was looking for a solution that could also be used for other shapes such as circles or more complicated shapes. – KatyB Jul 16 '14 at 14:26
  • 1
    @KatyB You can use Sigur's solution combined with clip for other shapes. – JLDiaz Jul 16 '14 at 14:34
12

Yes, it is possible. In this answer I will try to cover the vertical shading: the concepts can be applied in the same way to the horizontal shading. Radial shading, instead is a bit different and the idea has been already implemented in TikZ: radial shading of a ring to some extent.

The code:

\documentclass[tikz,border=10pt]{standalone}

\makeatletter
\tikzset{vertical custom shading/.code={%
 \pgfmathsetmacro\tikz@vcs@middle{#1}
 \pgfmathsetmacro\tikz@vcs@bottom{\tikz@vcs@middle/2}
 \pgfmathsetmacro\tikz@vcs@top{(100-\tikz@vcs@middle)/2+\tikz@vcs@middle}
\pgfdeclareverticalshading[tikz@axis@top,tikz@axis@middle,tikz@axis@bottom]{newaxis}{100bp}{%
  color(0bp)=(tikz@axis@bottom);
  color(\tikz@vcs@bottom bp)=(tikz@axis@bottom);
  color(\tikz@vcs@middle bp)=(tikz@axis@middle);
  color(\tikz@vcs@top bp)=(tikz@axis@top);
  color(100bp)=(tikz@axis@top)}
  \pgfkeysalso{/tikz/shading=newaxis}
  }
}
\makeatother  


\begin{document}

\begin{tikzpicture}
\draw[top color=red,
      bottom color=blue, 
      middle color=white,
      vertical custom shading=60]
 (0,0) rectangle (4,2);

\draw[top color=blue,
      bottom color=red, 
      middle color=white,
      vertical custom shading=35]
 (5,0) rectangle (9,2);
\end{tikzpicture}

\end{document}

The result:

enter image description here

  • Nice! So easy to understand the theory behind the codes. Math!! – Sigur Jul 16 '14 at 15:11
  • @Sigur: Indeed, you always need some math for drawings. :) – Claudio Fiandrino Jul 17 '14 at 6:23
  • @Claudio Fiandrino, how to execute this command within the scope with optional argument canvas transform={rotate around={10:(0,0)}}? – forrest Oct 11 '17 at 12:59

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.