0

I have an outer and an inner path in tikz. I would like to create a fading between them, so that at the outer path the color is completly balck and at the inner completly white. My problem is to create a fading that is not going to one middle point but is equal along both paths.

Here is a minimalistic version of my code:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}

\coordinate (A) at (1,-3);
\coordinate (B) at (7,-1);
\coordinate (C) at (9,-7);
\coordinate (D) at (3,-9);

\fill[even odd rule,inner color=white,outer color=black]
 %outer path
(A) arc (180:90:2) -- (B) arc (90:0:2) -- (C) arc (360:270:2) -- (D) arc (270:180:2) -- (A)
%inner path
($(A)+(1,0)$) arc (180:90:1) -- ($(B)+(0,-1)$) arc (90:0:1) -- 
($(C)+(-1,0)$) arc (360:270:1) -- ($(D)+(0,1)$) arc (270:180:1) -- ($(A)+(1,0)$);

\end{tikzpicture}
\end{document}

I would be very glad if someone could help me with this :-)

1
  • There is a tiny bit of fading but it is hard to see. The problem is that white is in the center but you only show a small strip away from the center. Maybe tex.stackexchange.com/a/493406 gives you what you want.
    – user229669
    Dec 1, 2020 at 13:58

1 Answer 1

1

It will not help you to declare a radial shading with the inner color, white further out. -as your area is not a ring, it looks strange:

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calc}
\pgfdeclareradialshading{ring}{\pgfpointorigin}{%
   rgb(0.8cm)=(1,1,1);
   rgb(1.4cm)=(0,0,0)}    
\begin{document}
\begin{tikzpicture}    
\coordinate (A) at (1,-3);
\coordinate (B) at (7,-1);
\coordinate (C) at (9,-7);
\coordinate (D) at (3,-9);    
\shade[shading=ring, even odd rule]
 %outer path
(A) arc (180:90:2) -- (B) arc (90:0:2) -- (C) arc (360:270:2) -- (D) arc (270:180:2) -- (A)
%inner path
($(A)+(1,0)$) arc (180:90:1) -- ($(B)+(0,-1)$) arc (90:0:1) -- 
($(C)+(-1,0)$) arc (360:270:1) -- ($(D)+(0,1)$) arc (270:180:1) -- ($(A)+(1,0)$);    
\end{tikzpicture}
\end{document}

Radial shaded rounded square

One option is to declare a functional shading, but it is much much simpler to just draw lines in different shades of gray. I observe, that your shape is just a rounded square, and use that instead of your original path.

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\foreach \i in {0,1,...,100}
\draw[black!\i, rounded corners=(1cm+\i/100 cm] (2-\i/100,-8-\i/100) rectangle (8+\i/100,-2+\i/100);
\end{tikzpicture}
\end{document}

Shaded rounded square

1
  • Thank you very much! I didn't thought of this way to do it myself but its working and easier to use then the fading options from tikz (for me at least). So perfect, thanks.
    – Tobias
    Dec 2, 2020 at 12:03

You must log in to answer this question.

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