TikZ – How to Create a Two-Colored Checkerboard Pattern

colorpatterntikz-pgf

I want to create a checkerboard pattern with two colors.
Is there a possibility to do this inside a pattern definition?

I found a node-based solution (first print the background, then add the foreground or better: use a combination with preaction and fill)

But I'm looking for a twocolored pattern.

My favorite solution would be a pattern with selectable colors, but pattern don't accept parameters (correct my, if I understood it wrong).

Example:

\documentclass[tikz]{standalone}
\usetikzlibrary{patterns}
\usepackage{luatex85}

% Flexible Checkerboards - based on the definition for checkerboard, but with different size.
% Define size of own check in pattern
\newlength{\flexcheckerboardsize}

%Define a small checkerboard
\setlength{\flexcheckerboardsize}{.5mm}
\pgfdeclarepatternformonly{smallcheckerboard}{\pgfpointorigin}{\pgfqpoint{2\flexcheckerboardsize}{2\flexcheckerboardsize}}{\pgfqpoint{2\flexcheckerboardsize}{2\flexcheckerboardsize}}%
  {
    \pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{\flexcheckerboardsize}{\flexcheckerboardsize}}
    \pgfpathrectangle{\pgfqpoint{\flexcheckerboardsize}{\flexcheckerboardsize}}{\pgfqpoint{\flexcheckerboardsize}{\flexcheckerboardsize}}
    \pgfusepath{fill}
  }

%Define a new checkerboard with predefined size and colors.
%Parameters: Name, size, color1, color2
\newcommand{\defineflexcheckerboard}[4]{
  \setlength{\flexcheckerboardsize}{#2}
  \pgfdeclarepatternformonly{#1}{\pgfpointorigin}{\pgfqpoint{2\flexcheckerboardsize}{2\flexcheckerboardsize}}{\pgfqpoint{2\flexcheckerboardsize}{2\flexcheckerboardsize}}%
  {
    \pgfsetfillcolor{#4}%no effect
    \pgfsetcolor{#3}%ok, but option pattern color is ignored
    \pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{\flexcheckerboardsize}{\flexcheckerboardsize}}
    \pgfpathrectangle{\pgfqpoint{\flexcheckerboardsize}{\flexcheckerboardsize}}{\pgfqpoint{\flexcheckerboardsize}{\flexcheckerboardsize}}
    \pgfusepath{fill}
  }
}

\defineflexcheckerboard{flexcheckerboard_bw}{.5mm}{black}{white}
\defineflexcheckerboard{flexcheckerboard_redblue}{.5mm}{red}{blue}

\begin{document}
  \begin{tikzpicture}[node distance=5mm]
  %background is not used
  \node (c0bw) [circle, radius = 1cm, pattern=flexcheckerboard_bw] {};
  \node (c0rb) [circle, radius = 1cm, pattern=flexcheckerboard_redblue,right of=c0bw] {}; %Wrong no blue

  %This are two work arounds: Paint background, then pattern
  \node (c1) [circle, radius = 1cm, blue, fill, below of=c0bw] {};
  \node (c1) [circle, radius = 1cm, pattern=flexcheckerboard_redblue, below of=c0bw] {};
  %Same with a preaction
  \node (c1b) [preaction={fill, blue},circle, radius = 1cm, right of=c1, pattern=flexcheckerboard_redblue] {};

  %Without any color definition in the pattern, I can influence both.
  \node [circle, radius = 1cm, below of=c1,pattern=smallcheckerboard,pattern color=green,preaction={fill, blue}] {};
  \end{tikzpicture}
\end{document}
  • First row: This pattern should be two-colored
  • 2nd row: Work arounds (I don't want this work around)
  • 3rd row: No color definition in the pattern, colors are controlled from outside.
    enter image description here

Background why I don't want the work around: I'm preparing a new tikzduck-decoration and I want to use it with a pattern. Without the multi-color-pattern I must define additional parameters in tikzduck to handle the different colors.

Best Answer

You can start from already defined checkerboard light grey which uses two colors. It draws a large square and two smaller squares with a second color on it. Based is this definition and adapting it to your code is easy to do

\documentclass[tikz]{standalone}
\usetikzlibrary{patterns}
\usepackage{luatex85}

% Flexible Checkerboards - based on the definition for checkerboard, but with different size.
% Define size of own check in pattern
\newlength{\flexcheckerboardsize}

%Define a new checkerboard with predefined size and colors.
%Parameters: Name, size, color1, color2
\newcommand{\defineflexcheckerboard}[4]{
    \setlength{\flexcheckerboardsize}{#2}
    \pgfdeclarepatterninherentlycolored{#1}
        {\pgfpointorigin}{\pgfqpoint{2\flexcheckerboardsize}    
        {2\flexcheckerboardsize}}
        {\pgfqpoint{2\flexcheckerboardsize}
        {2\flexcheckerboardsize}}%
        {
            \pgfsetfillcolor{#4}
            \pgfpathrectangle{\pgfpointorigin}{
            \pgfqpoint{2.1\flexcheckerboardsize}    
                {2.1\flexcheckerboardsize}}% make
                                % slightly larger to ensure that tiles
                                % are really solid
          \pgfusepath{fill}
          \pgfsetfillcolor{#3}
          \pgfpathrectangle{\pgfpointorigin}
            {\pgfqpoint{\flexcheckerboardsize}
            {\flexcheckerboardsize}}
          \pgfpathrectangle{\pgfqpoint{\flexcheckerboardsize}
            {\flexcheckerboardsize}}
            {\pgfqpoint{\flexcheckerboardsize}
            {\flexcheckerboardsize}}
            \pgfusepath{fill}
        }
}

\defineflexcheckerboard{flexcheckerboard_bw}{.5mm}{black}{white}
\defineflexcheckerboard{flexcheckerboard_redblue}{.5mm}{red}{blue}
\defineflexcheckerboard{flexcheckerboard_greenorange}{1mm}{green}{orange}
\defineflexcheckerboard{flexcheckerboard_bluecyan}{.2mm}{cyan}{blue}

\begin{document}
  \begin{tikzpicture}[node distance=5mm]
  %background is not used
  \node (c0bw) [circle, radius = 1cm, pattern=flexcheckerboard_bw] {};
  \node (c0rb) [circle, radius = 1cm, pattern=flexcheckerboard_redblue,right of=c0bw] {}; %Wrong no blue
  \node (c0go) [circle, radius = 1cm, pattern=flexcheckerboard_greenorange,below of=c0bw] {}; %Wrong no blue
  \node (c0og) [circle, radius = 1cm, pattern=flexcheckerboard_bluecyan,below of=c0rb] {};   \end{tikzpicture}
\end{document}

enter image description here

In Tikz : rotate a fill pattern some other bicolor patterns are defined. They can also be adapted to your syntax.