In the following example:

\shade[bottom color=cyan!60!black, top color=red, middle color = blue!20!white] (0,0) rectangle (4,5);

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

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:


\tikzset{vertical custom shading/.code={%
  color(\tikz@vcs@bottom bp)=(tikz@axis@bottom);
  color(\tikz@vcs@middle bp)=(tikz@axis@middle);
  color(\tikz@vcs@top bp)=(tikz@axis@top);


\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);


The result:

enter image description here

| improve this answer | |
  • 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.