5

I try to do a cover for a documentation.

For this, I need to do a shading between two colors. And to be more elegant I would like to make a halftone shading with TikZ, like shown in the picture.

halftone example

1
  • 2
    Welcome to TeX.SE! What have you tried so far? As it stands now it is not clear what you have troubles with? Is it the circles dwindling appearance or the back-shading? Furthermore, the practice on this site is to help others help you. We do not encourage "Do this for me" questions.
    – nickpapior
    Dec 17, 2013 at 8:56

2 Answers 2

10

A first solution may come with the decorations.shapes library.

The code:

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

The result:

enter image description here

Second solution

Without using the slow decorations.shapes library, one might proceed with a nested loop as follow:

\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}
5
  • I have in mind a faster solution (meaning: no decoration library involved slowing down the compilation time). ASAP I will implement and update the answer. Dec 17, 2013 at 14:37
  • By the way, you can use PGFmath functions in the specification of coordinates: \path […]({iseven(\xi)*.15},\x)--(2,\x);. I think you get faster if you use line cap=round and a dash pattern with a zero-length on phase. Dec 17, 2013 at 14:43
  • @Qrrbrbirlbel Is iseven coming from CVS version? I can't find it in the pgfmanual. Dec 17, 2013 at 14:50
  • Oh! Yes, it is. I’m sorry. But there is still mod(\xi,2)==0 which can substitute for iseven(\xi). Dec 17, 2013 at 14:55
  • @Qrrbrbirlbel: no problems. Moreover, it is always possible to define own PGFmath functions ;) But it's nice to know it exists, thanks: I needed something similar for sa-tikz and I had to define my own \pgfmathisodd. Dec 17, 2013 at 15:04
1

Thanks for help, based on your solution I have done this piece of code :

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

I think we can do more proper code by extraction the (1cm) length into a lenth, set the even xshift by 0.5*\nodeSepLength

extract the number of circle into a counter, extracting the growing factor and the heigth factor into another two counter and dont repeate the \path line 2 time (set it after condition with a counter setted in each iteration). But it's work and it's look preaty good ;)

result of code

You must log in to answer this question.

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