Custom Fading for Rectangle

I tried to learn from

and then create my own fading scheme in this MWE:

\documentclass[tikz]{standalone}
{color(0cm)=(transparent!0);
color(0.7cm)=(transparent!0);
color(0.74cm)=(transparent!50);
color(0.82cm)=(transparent!70);
color(0.9cm)=(transparent!100)}
\begin{document}
\begin{tikzpicture}
\fill[blue] (0,0) rectangle (3,3);
\node[rectangle,fill=white,path fading=fade original,align=center,rounded corners] at (1.5,2) {\small this is text};
\node[rectangle,fill=white,path fading=test,align=center,rounded corners] at (1.5,1) {\small this is text};
\end{tikzpicture}
\end{document}

This is the relevant part of the image produced If I can't achieve this, I'd be able to use the radial fading if I could start the fading outside the text and fade to transparent!100 more abruptly, but the rectangular version would be ideal.

• I can't help but think that there must surely be a simple way to do this.... It just seems so... basic. – cfr Jan 17 '16 at 3:07

Here are four possibilities. None of them are perfect. The first two are similar and use tikzfadingfrompicture to draw rectangles which decrease in size as they increase in opacity. The only difference is the point at which the rectangles cease to be wholly opaque. In the first case, the central area of opacity is larger than in the second.

\foreach \i  [evaluate=\i as \k using {1.5-\i/100} ] in {0,1,...,100} \path [fill=white!\i!black] (-\k,-\k) rectangle (\k,\k);
\foreach \i  [evaluate=\i as \k using {1.25-\i/100} ] in {0,1,...,100} \path [fill=white!\i!black] (-\k,-\k) rectangle (\k,\k);

The second two use \pgfdeclareshading. I don't think this makes any real difference and you could use tikzfadingfrompicture to the same effect as far as I can tell, but I happened to do it this way. In this case, the idea is to shade four triangles which meet in the centre of a square when the shading is defined. So the top triangle is opaque at the bottom apex and transparent at the top base; the bottom triangle is opaque at the top apex and transparent at the bottom base; the left triangle is opaque at the right apex and transparent at the left base; and the right triangle is opaque at the left apex and transparent at the right base.

This is much more efficient, as far as I can tell, because it involves shading 4 triangles rather than filling 101 paths. However, I get artefacts when the result is rendered. This is especially problematic for the first example but visible in both. This may depend on PDF viewer.

The difference between the two is that the second specifies a middle colour as opaque, as well as the colour at the apex, so that there is a central opaque area before the shading begins. This also means it is possible to partly compensate for the artefacts by explicitly drawing the diagonals of the central area with opaque colour after shading the triangles. However, the artefacts are still visible along the diagonals outside this central area.

\tikz {
\shade [bottom color=pgftransparent!100, top color=pgftransparent!0] (0,0) -- (1,-1) -- (-1,-1) -- cycle;
\shade [bottom color=pgftransparent!0, top color=pgftransparent!100] (0,0) -- (-1,1) -- (1,1) -- cycle;
\shade [right color=pgftransparent!0, left color=pgftransparent!100] (0,0) -- (-1,1) -- (-1,-1) -- cycle;
\shade [right color=pgftransparent!100, left color=pgftransparent!0] (0,0) -- (1,1) -- (1,-1) -- cycle;
}
}
\tikz {
\shade [bottom color=pgftransparent!100, top color=pgftransparent!0, middle color=pgftransparent!0] (0,0) -- (1,-1) -- (-1,-1) -- cycle;
\shade [bottom color=pgftransparent!0, top color=pgftransparent!100, middle color=pgftransparent!0] (0,0) -- (-1,1) -- (1,1) -- cycle;
\shade [right color=pgftransparent!0, left color=pgftransparent!100, middle color=pgftransparent!0] (0,0) -- (-1,1) -- (-1,-1) -- cycle;
\shade [right color=pgftransparent!100, left color=pgftransparent!0, middle color=pgftransparent!0] (0,0) -- (1,1) -- (1,-1) -- cycle;
\draw [pgftransparent!0] (-.5,.5) -- (.5,-.5) (-.5,-.5) -- (.5,.5);
}
}

Here's a demonstration of the 4 fadings: Complete code:

\documentclass[tikz,border=5pt,multi]{standalone}
\foreach \i  [evaluate=\i as \k using {1.5-\i/100} ] in {0,1,...,100} \path [fill=white!\i!black] (-\k,-\k) rectangle (\k,\k);
\foreach \i  [evaluate=\i as \k using {1.25-\i/100} ] in {0,1,...,100} \path [fill=white!\i!black] (-\k,-\k) rectangle (\k,\k);
\tikz {
\shade [bottom color=pgftransparent!100, top color=pgftransparent!0] (0,0) -- (1,-1) -- (-1,-1) -- cycle;
\shade [bottom color=pgftransparent!0, top color=pgftransparent!100] (0,0) -- (-1,1) -- (1,1) -- cycle;
\shade [right color=pgftransparent!0, left color=pgftransparent!100] (0,0) -- (-1,1) -- (-1,-1) -- cycle;
\shade [right color=pgftransparent!100, left color=pgftransparent!0] (0,0) -- (1,1) -- (1,-1) -- cycle;
}
}
\tikz {
\shade [bottom color=pgftransparent!100, top color=pgftransparent!0, middle color=pgftransparent!0] (0,0) -- (1,-1) -- (-1,-1) -- cycle;
\shade [bottom color=pgftransparent!0, top color=pgftransparent!100, middle color=pgftransparent!0] (0,0) -- (-1,1) -- (1,1) -- cycle;
\shade [right color=pgftransparent!0, left color=pgftransparent!100, middle color=pgftransparent!0] (0,0) -- (-1,1) -- (-1,-1) -- cycle;
\shade [right color=pgftransparent!100, left color=pgftransparent!0, middle color=pgftransparent!0] (0,0) -- (1,1) -- (1,-1) -- cycle;
\draw [pgftransparent!0] (-.5,.5) -- (.5,-.5) (-.5,-.5) -- (.5,.5);
}
}
\begin{document}
\begin{tikzpicture}
\fill[blue] (-1.5,-1.5) rectangle (1.5,1.5);
\node [align=center, rounded corners, fill=white, path fading=rectangle fade] at (0,1) {\small this is text};
\node [align=center, rounded corners, fill=white, path fading=rectangle fading] at (0,.33) {\small this is text};
\node [align=center, rounded corners, fill=white, path fading=rectangular fading] at (0,-.33) {\small this is text};
\node [align=center, rounded corners, fill=white, path fading=rectangular fade] at (0,-1) {\small this is text};
\end{tikzpicture}
\end{document}
• okay, i like you idea. split it up into subdomains, each of which have their own fading. i will fiddle with this for a couple of weeks. will report back. – aeroNotAuto Jan 17 '16 at 7:36