如何用 tikz 创建双色棋盘格图案?

如何用 tikz 创建双色棋盘格图案?

我想创建一个有两种颜色的棋盘图案。是否可以在图案定义中执行此操作?

我找到了一个基于节点的解决方案(首先打印背景,然后添加前景或更好的方法:使用预动作和填充的组合)

但我正在寻找双色图案。

我最喜欢的解决方案是带有可选颜色的图案,但是模式不接受参数(如果我理解错了,请纠正我)。

例子:

\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}
  • 第一行:此图案应为双色
  • 第二行:解决方法(我不想要这个解决方法)
  • 第三行:图案中没有颜色定义,颜色由外部控制。 在此处输入图片描述

我不想采用这种解决方法的背景:我正在准备一个新的 tikzduck 装饰,我想将它与图案一起使用。如果没有多色图案,我必须在 tikzduck 中定义其他参数来处理不同的颜色。

答案1

您可以从已定义的checkerboard light grey使用两种颜色开始。它绘制一个大正方形和两个较小的正方形,上面有第二种颜色。基于此定义,将其调整到您的代码中很容易

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

在此处输入图片描述

Tikz:旋转填充图案还定义了一些其他的双色模式。它们也可以适应您的语法。

答案2

稍微阐述一下我的评论(并使用这个答案)。钛Z 已经有一个图案颜色,存储在 中\tikz@pattern@color。(要使用它,我将图案放在\makeatletter环境中。)我认为使用背景没有任何问题,您可以在后续操作中添加图案并获得漂亮的双色图案。然而,在我了解到这可能在著名的 tikzducks 包中用到之后,我重写了代码,使其具有两个功能:要么绘制双色棋盘,要么在背景上只绘制一种颜色(checker board background=none,这是预定义设置)。您可以使用 pgfkeys 调整所有内容,就像在普通图案中一样。

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

在此处输入图片描述

相关内容