How to create a twocolored checkerboard pattern with tikz?
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}
In Tikz : rotate a fill pattern some other bicolor patterns are defined. They can also be adapted to your syntax.
Just spelling out my comment a bit (and using this answer). TikZ has already a pattern color, which gets stored in \tikz@pattern@color
. (To use it, I put the pattern in a \makeatletter
environment.) And I do not see anything wrong with using a background, you can add the pattern in a postaction and get beautiful two-colored patterns. However, after I learned that this might potentially be used in the famous tikzducks package, I rewrote my code to come with two functionalities: either draw a two-color checkerboard or just one color on a background (checker board background=none
, which is the predefined setting). You can adjust all the things with pgfkeys, just like in ordinary patterns.
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}
\makeatletter
\tikzset{/tikz/.cd,
checker board size/.store in=\flexcheckerboardsize,
checker board size=6pt,
checker board background/.store in=\flexcheckerboardbgd,
checker board background=none
}
\pgfdeclarepatterninherentlycolored[\flexcheckerboardsize,\flexcheckerboardbgd]{flex checker board}
{\pgfqpoint{-1pt}{-1pt}}
{\pgfqpoint{\flexcheckerboardsize}{\flexcheckerboardsize}}
{\pgfqpoint{\flexcheckerboardsize}{\flexcheckerboardsize}}
{\pgfmathsetmacro{\tmp}{0.5*\flexcheckerboardsize}
\pgfsetcolor{\tikz@pattern@color}
\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{\tmp pt}{\tmp pt}}
\pgfpathrectangle{\pgfqpoint{\tmp pt}{\tmp pt}}{\pgfqpoint{\tmp pt}{\tmp pt}}
\pgfusepath{fill}
\def\none{none}
\ifx\flexcheckerboardbgd\none%
\else
\pgfsetcolor{\flexcheckerboardbgd}
\pgfpathrectangle{\pgfqpoint{0pt}{\tmp pt}}{\pgfqpoint{\tmp pt}{\tmp pt}}
\pgfpathrectangle{\pgfqpoint{\tmp pt}{0pt}}{\pgfqpoint{\tmp pt}{\tmp pt}}
\pgfusepath{fill}
\fi
}
\makeatother
\begin{document}
\begin{tikzpicture}
\draw [checker board size=9pt, pattern=flex checker board, pattern color =
red] (0,0) circle[radius = 1cm];
\draw [checker board background=blue,checker board size=12pt, pattern=flex checker board, pattern color =
red] (2,0) circle[radius = 1cm];
\draw [fill=orange,postaction={checker board background=none,checker board size=12pt, pattern=flex checker board, pattern color =
red}] (4,0) circle[radius = 1cm];
\end{tikzpicture}
\end{document}