5

Consider the following example:

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,calc,shapes,arrows,shapes.geometric,patterns,shadows,arrows.meta,fadings}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage[T1]{fontenc}
\usepackage{calc}
\definecolor{sea}{HTML}{80A0C0}

\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}

\begin{scope}
% region to clip
\coordinate(clipping area) at (8, 15);
\clip (2,5) rectangle (clipping area);
% draw water
\fill [decoration={snake, segment length=1cm, amplitude=0.125cm}, decorate, top color = red, bottom color = sea, middle color = sea, vertical custom shading = 65] (0,0) rectangle (10,10);
\end{scope}

\begin{scope}[xshift = 200]
% region to clip
\coordinate(clipping area) at (8, 15);
\clip (2,5) rectangle (clipping area);
% draw water
\fill [decoration={snake, segment length=1cm, amplitude=0.125cm}, decorate, top color = red, bottom color = sea, middle color = sea, vertical custom shading = 65] (0,0) rectangle (10,10);
\end{scope}

\end{tikzpicture}
\end{document}

which results in:

enter image description here

I would like to change the location of the shading in the second panel so that the blue region extends to the top one one side and an equal amount of the red is moved in the opposite direction on the other side. I have tried to illustrate this by annotating the diagram:

enter image description here

i.e. red region goes up on one side and down an equal amount on the other side. How can I achieve this affect?

6

I would go about this differently: Instead of clipping a large shaded rectangle, I would use a single path and decorate only one segment of that path, like in tikz apply fill between two lines. That way, you can use a "normal" fading instead of the custom fading from your example.

In order to get a nice wave pattern without a straight segment at one end, I use Nicer wavy line with TikZ.

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,calc,shapes,arrows,shapes.geometric,patterns,shadows,arrows.meta,fadings}
\definecolor{sea}{HTML}{80A0C0}

\pgfdeclaredecoration{complete sines}{initial}
{
    \state{initial}[
        width=+0pt,
        next state=sine,
        persistent precomputation={\pgfmathsetmacro\matchinglength{
            \pgfdecoratedinputsegmentlength / int(\pgfdecoratedinputsegmentlength/\pgfdecorationsegmentlength)}
            \setlength{\pgfdecorationsegmentlength}{\matchinglength pt}
        }] {}
    \state{sine}[width=\pgfdecorationsegmentlength]{
        \pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
        \pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
        \pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
        \pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
}
    \state{final}{}
} 

\begin{document}
\begin{tikzpicture}

\begin{scope}
\fill [decoration={complete sines, segment length=1cm, amplitude=0.125cm}, top color = red, bottom color = sea, middle color = sea] decorate {(0,0) -- (10,0)} |- (0,-2) -- cycle;
\end{scope}

\begin{scope}[xshift=11cm]
\fill [decoration={complete sines, segment length=1cm, amplitude=0.125cm}, top color = red, bottom color = sea, middle color = sea, shading angle=-20] decorate {(0,0) -- (10,0)} |- (0,-2) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}

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.