在 \foreach 循环中改变颜色

在 \foreach 循环中改变颜色

我使用了以下代码,来自答案这个问题

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\begin{document}
\begin{frame}[t]
\frametitle{Spirograph 1--}
\begin{tikzpicture}[pics/fcross/.style={code={
\path[fill=blue!40!white, looseness=1,pic actions] 
(0,0) node {x} (0,-2) foreach \X in {0,90,180,270}
{[rotate=\X] -- (0,-2) to [out=0,in=-120] ++ (0.2,0.1) to [out=60,in=-150] ++ (1.7,1.7) to [out=30,in=-90] ++ (0.1,0.2)} -- cycle;
}}]
 \begin{scope}[blend group=lighten,fill opacity=.4]
  \path foreach \Y in {0,30,60} {pic[rotate=\Y,scale=2,line width=.2cm,draw=blue] {fcross}};
 \end{scope} 
\end{tikzpicture}
\end{frame}
\begin{frame}[t]
\frametitle{Spirograph 1}
\begin{tikzpicture}[pics/fcross/.style={code={
\path[line width=.1cm,fill=blue!40!white, looseness=1,pic actions] 
(0,-2) foreach \X in {0,90,180,270}
{[rotate=\X] -- (0,-2) to [out=0,in=-120] ++ (0.2,0.1) to [out=60,in=-150] ++ (1.7,1.7) to [out=30,in=-90] ++ (0.1,0.2)} -- cycle;
}}]
 \begin{scope}[blend group=lighten,fill opacity=.4]
  \path foreach \Y in {0,30,60} {pic[rotate=\Y,scale=2] {fcross}};
 \end{scope} 
 \path foreach \Y in {0,30,60} {pic[rotate=\Y,scale=2,fill=none,draw=blue!40] {fcross}};
\end{tikzpicture}
\end{frame}
\end{document}

如何让重复的绘图的填充颜色渐变,或者产生以下绘图

在此处输入图片描述

在此处输入图片描述

答案1

之前你在这个上下文中提出的问题可以用适当的 来回答blend group。在这里,我认为这不再是这种情况。相反,人们可以使用clip。不同的颜色可以在循环中构建,如下所示Colo 提及。下面是一些例子。整个故事现在有点让人想起实验venndiagrams图书馆,但当然clip已被广泛使用。

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\tikzset{contour/.style={insert path={(0,-2) foreach \X in {0,90,180,270}
{[rotate=\X] -- (0,-2) to [out=0,in=-120] ++ (0.2,0.1) to [out=60,in=-150] ++ (1.7,1.7) to [out=30,in=-90] ++ (0.1,0.2)} -- cycle
}}}
\begin{document}
\begin{frame}[t]
\frametitle{Spirograph 1}
\begin{tikzpicture}
\begin{scope}[local bounding box=L]
  \foreach \Y [count=\Z starting from 0]
  in {red,blue,blue!50} 
  {\draw[fill=\Y,rotate=\Z*30,contour,fill opacity=0.5]; }
  \foreach \Y in {0,1,2}
  {\begin{scope}
   \foreach \Z  in {0,1,2}
   {\ifnum\Y=\Z
   \else
    \path[clip,rotate=\Z*30,contour];
   \fi}
   \fill[yellow] (L.south west) rectangle (L.north east);
  \end{scope}}
  \foreach \Z  in {0,1,2}
  {\draw[draw=blue!40,line width=0.5mm,rotate=\Z*30,contour]; }
\end{scope}  
\begin{scope}[local bounding box=R,xshift=5cm]
  \foreach \Y [count=\Z starting from 0]
  in {red,blue,blue!50} 
  {\draw[fill=\Y,draw=blue!40,line width=0.5mm,rotate=\Z*30,contour,fill opacity=0.5]; }
  \foreach \Y in {0,1,2}
  {\begin{scope}
   \foreach \Z  in {0,1,2}
   {\ifnum\Y=\Z
   \else
    \path[clip,rotate=\Z*30,contour];
   \fi}
   \fill[cyan!40] (R.south west) rectangle (R.north east);
  \end{scope}}
\end{scope}  
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

答案2

一个metapost解决方案。圆角是最大的问题。 在此处输入图片描述

\documentclass{article}
\usepackage{luamplib}
\begin{document}
\mplibnumbersystem{double}
\mplibforcehmode
\begin{mplibcode}
u:=1cm; r:=.2cm;
path p[]; pen mypen; color c[];
c1=.7[red,white];
c2=.7[blue,white];
c3=.4[red,white];
c4=(.99,.93,0);
% envelope needs a polygonal pen, so approximate a circular pen
p[-1]:= (r,0) for i=1 upto 120: --(r,0) rotated 3i endfor--cycle;
mypen:= makepen(p[-1]);
pickup mypen;
% set coords so that outer edge of pen hits them, not center
rt z0=u*(2,0);
top z1=u*(0,2);
lft z2=u*(-2,0);
bot z3=u*(0,-2);
p0=z0{dir 150}..{dir 120}z1{dir 240}..{dir 210}z2{dir 330}..{dir 300}z3{dir 60}..{dir 30}z0..cycle;
% draw path with pen of radius=rounded corner radius, envelope=outer boundary of this path
p1=envelope mypen of p0;
% remainder is fairly self explanatory.
p2=p1 rotated 30;
p3=p2 rotated 30;
z4=p1 intersectiontimes p2;
z5=p1 intersectiontimes p3;
p4=subpath (xpart z4-eps,xpart z5+eps) of p1;
p5=subpath (ypart z5-eps,ypart z4+eps) of p3;
p6=subpath (ypart z4-eps,length p2+xpart z4+eps) of p2;
p7=buildcycle(p4,p5,p6);

beginfig(0);
for i=1 upto 3: fill p[i] withcolor c4; endfor;
for i=0 upto 3:
    for j=0 upto 3:
        fill p7 rotated (30i+90j) withcolor c[i];
    endfor;
endfor;
for i=1 upto 3: draw p[i] withcolor .5[blue,white]; endfor;
endfig;

beginfig(1);
for i=1 upto 3: fill p[i] withcolor .8[blue,white]; endfor;
for i=0 upto 3:
    for j=0 upto 3:
        fill p7 rotated (30i+90j) withcolor c[i];
        draw p7 rotated (30i+90j) withcolor .5[blue,white];
    endfor;
endfor;
endfig;
\end{mplibcode}
\end{document}

相关内容