使用 TikZ 进行半色调着色

使用 TikZ 进行半色调着色

我尝试为一份文档做封面。

为此,我需要在两种颜色之间进行阴影处理。为了更优雅,我想使用 TikZ 进行半色调阴影处理,如图所示。

半色调示例

答案1

第一个解决方案可能随decorations.shapes库而来。

代码:

\documentclass[tikz,border=10pt]{standalone}

\usepackage{etoolbox,tikz}
\usetikzlibrary{decorations.shapes}

\tikzset{decorate with/.style args={#1 with size #2}{
    decorate,decoration={shape backgrounds,shape=circle,shape size=#2},
    fill=#1,
  }
}           

\begin{document}

\begin{tikzpicture}
\clip(0,0) rectangle(2,8);
\fill[black] (0,0) rectangle(2,8);
\shade[top color=black,middle color=black!10,bottom color=white](0,0) rectangle(2,2);

\foreach \x[count=\xi] in {1.7,1.45,...,0}{
  \ifnumodd{\xi}{%true
    \path [decorate with={black with size 1.2*\x mm}](0,\x)--(2,\x);
  }{%false
    \path [decorate with={black with size 1.2*\x mm}](0.15,\x)--(2,\x);
  }
}
\end{tikzpicture}

\end{document}

结果:

在此处输入图片描述

第二种解决方案

如果不使用慢速decorations.shapes库,可以按如下方式进行嵌套循环:

\documentclass[tikz,border=10pt]{standalone}

\usepackage{tikz}

\makeatletter
% iseven function, taken from CVS
\pgfmathdeclarefunction{iseven}{1}{%
  \begingroup
    \pgfmathsetcount\c@pgfmath@counta{abs(int(#1))}%
    \ifodd\c@pgfmath@counta
      \def\pgfmathresult{0}%
    \else
      \def\pgfmathresult{1}%
    \fi
    \pgfmath@smuggleone\pgfmathresult
  \endgroup}
\makeatother 

\begin{document}

\begin{tikzpicture}
\clip(0,0) rectangle(2,8);
\fill[black] (0,0) rectangle(2,8);
\shade[top color=black,middle color=black!10,bottom color=white](0,0) rectangle(2,2);

\foreach \y[count=\yi] in {1.7,1.4,...,0}{
    \pgfmathparse{iseven(\yi)*0.2}
    \pgfmathsetmacro\init{\pgfmathresult}% the start changes accordingly
    % to the line (even or odd)
    \pgfmathsetmacro\iinit{\pgfmathresult+0.4} % we declare the next step
    % that is the distance between circles in the same line
    \foreach \x in {\init,\iinit,...,2}{
            \fill (\x,\y) circle[radius=0.1*\y cm];
        }
}
\end{tikzpicture}

\end{document}

答案2

谢谢您的帮助,根据您的解决方案我完成了这段代码:

\documentclass[tikz,border=10pt,a4paper]{article}

\usepackage{etoolbox,tikz}
\usetikzlibrary{decorations}
\usetikzlibrary{decorations.shapes}
\usetikzlibrary{calc}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\tikzset{decorate with/.style args={#1 with size #2}{
    decorate,decoration={shape backgrounds,shape=circle,shape size=#2,shape sep=1cm},
    fill=#1
  }
} 
\newcommand{\pagePrincipale}[2]{%
  \begin{tikzpicture}[remember picture,overlay]
    \path[fill=blue!60] (current page.north west) -- (current page.north east) 
                                                 -- (current page.south east) -- (current page.south west) -- (current page.north west) -- cycle;
    \path (current page.south east) ++(0cm, 12cm) coordinate (cyan left begin);
    \path (current page.south west) ++(0cm, 12cm) coordinate (cyan right begin);
    \path[fill=cyan!40] (cyan right begin) -- (cyan left begin) 
                                                 -- (current page.south east) -- (current page.south west) -- (cyan right begin) -- cycle;
    \begin{pgfonlayer}{foreground}
    \foreach \x[count=\xi] in {1,2,...,8}{
      \ifnumodd{\xi}{%true
        \path [decorate with={blue!60 with size \x .25mm}]  (0cm, \x * 0.4) ++ (0cm, 8.75cm) + (current page.south west)  -- +(current page.south east);
      }{%false
        \path [decorate with={blue!60 with size \x .25mm}]  (.5cm, \x * 0.4) ++ (0cm, 8.75cm) + (current page.south west) -- +(current page.south east);
      }
    }
    \end{pgfonlayer}
    \node[rectangle,anchor=south,text=white,font={\Huge\bfseries},yshift=0.2cm] (title) at (current page.center) {#1};
    \node[rectangle,anchor=north,text=white,font=\huge,yshift=-0.2cm] (subtitle) at (current page.center) {#2};
    \draw[draw=white,line width=2pt] (-0.5*\textwidth, 0.5cm) ++ (title.north) -- ++(\textwidth, 0cm);
    \draw[draw=white,line width=2pt] (-0.5*\textwidth, -0.5cm) ++ (subtitle.south) -- ++(\textwidth, 0cm);
  \end{tikzpicture}
  \cleardoublepage
}

\begin{document}
\pagePrincipale{AAA}{BBB}
\end{document}

我认为我们可以通过将(1cm)长度提取为长度来编写更合适的代码,将偶数 xshift 设置为 0.5*\nodeSepLength

将圆的数量提取到一个计数器中,将增长因子和高度因子提取到另外两个计数器中,不要重复 \path 行 2 次(在每次迭代中设置计数器后设置条件)。但它有效并且看起来很棒 ;)

代码结果

相关内容