是否可以定义填充中顶部、底部和中间颜色的位置?

是否可以定义填充中顶部、底部和中间颜色的位置?

在以下示例中:

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

是否可以不将“中间颜色”放置在矩形的中间,而是将其向上移动四分之一,以便“中间颜色”在向上四分之三的位置将“顶部”和“底部”颜色分开?

答案1

是的,这是可能的。在这个答案中,我将尝试介绍垂直阴影:这些概念可以以相同的方式应用于水平阴影。相反,径向阴影有点不同,这个想法已经在TikZ:环的径向阴影在某种程度上。

代码:

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

结果:

在此处输入图片描述

相关内容