编辑

编辑

为什么在以下所有形状的 MWE 中,尽管定义不同,但图案颜色都与第一个一样?

%%%% pattern-color
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{chains,patterns,backgrounds}
\makeatletter
\tikzset{
        hatch distance/.store in=\hatchdistance,
        hatch distance=5pt,
        hatch thickness/.store in=\hatchthickness,
        hatch thickness=5pt
        }
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{north east hatch}% name
    {\pgfqpoint{-1pt}{-1pt}}% below left
    {\pgfqpoint{\hatchdistance}{\hatchdistance}}% above right
    {\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
    {
        \pgfsetcolor{\tikz@pattern@color}
        \pgfsetlinewidth{\hatchthickness}
        \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
        \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
        \pgfusepath{stroke}
    }
\makeatother

\begin{document}
 \begin{tikzpicture}[
    start chain = going below,
  node distance = 2mm,
    Node/.style = {minimum width=#1,
                   shape=rectangle,
                   draw, fill=white,
                   on chain},
 Pattern/.style = {pattern=north east hatch,
                    pattern color=#1,%teal!30,
                    hatch distance=7pt,
                    hatch thickness=3pt},
    font=\small\sffamily]
%----------------
\node[Node=44mm,Pattern=red!30]     {desired pattern color: red};
\node[Node=44mm,Pattern=cyan!30,
      preaction={fill=yellow}]      {desired pattern color: cyan};
\node[Node=44mm]                    {without pattern};
\node[Node=44mm,Pattern=orange!30,
      preaction={fill=gray!30}]     {desired pattern color: orange};
%---
 \end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

这是 中的一个错误\pgfcorepatterns.code.tex。内部宏\pgf@declarepatternmutable将模式类型保存为 而7不是#7。更正后的行应为:

\expandafter\gdef\csname pgf@pattern@type@#1\endcsname{#7}%    

经过这种改变,模式就可以按预期发挥作用。

答案2

这是一个诊断,但不是解决方案。

仅形式模式的代码应该不是根本不包括颜色代码。因此,如果文档可信的话,在定义中设置颜色肯定是不行的。

然而,这只会让一切变成黑色或白色......

好的。从手册第 1064 页的模式代码开始stars。正如广告宣传的那样,效果很好:

黑色和红色

现在将变量添加到其中,以便我们有:

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{patterns}
\tikzset{
  hatch distance/.store in=\hatchdistance,
  hatch distance=5pt,
  hatch thickness/.store in=\hatchthickness,
  hatch thickness=5pt
}

\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{stars}
{\pgfpointorigin}
{\pgfpoint{1cm}{1cm}}
{\pgfpoint{1cm}{1cm}}
{
  \pgftransformshift{\pgfpoint{.5cm}{.5cm}}
  \pgfpathmoveto{\pgfpointpolar{0}{4mm}}
  \pgfpathlineto{\pgfpointpolar{144}{4mm}}
  \pgfpathlineto{\pgfpointpolar{288}{4mm}}
  \pgfpathlineto{\pgfpointpolar{72}{4mm}}
  \pgfpathlineto{\pgfpointpolar{216}{4mm}}
  \pgfpathclose%
  \pgfusepath{fill}
}

\begin{document}
  \begin{tikzpicture}
    \filldraw[pattern=stars] (0,0) rectangle (1.5,2);
    \filldraw[pattern=stars,pattern color=red](1.5,0) rectangle (3,2);
  \end{tikzpicture}
\end{document}

一切都不那么美好:

全黑

因此它并不像广告中说的那样有效...

还有一个关于手册这部分代码不起作用的问题。我会看看能不能找到它(我问过)。不是同一个问题,但也许它会提供线索。

编辑

并不是说不可能改变颜色......

\documentclass[tikz,border=5mm]{standalone}

\usetikzlibrary{patterns,chains}

\pgfdeclarepatternformonly{north east hatch}% name
{\pgfqpoint{-1pt}{-1pt}}% below left
{\pgfqpoint{7pt}{3pt}}% above right
{\pgfpoint{6pt}{6pt}}%
{
  \pgfsetlinewidth{3pt}
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{7pt}{3pt}}
  \pgfpathclose
  \pgfusepath{stroke}
}


\begin{document}
  \begin{tikzpicture}[
    start chain = going below,
    node distance = 2mm,
    Node/.style =
    {
      minimum width=#1,
      shape=rectangle,
      draw, fill=white,
      on chain
    },
    Pattern/.style =
    {
      pattern=north east hatch,
      pattern color=#1
    },
    font=\small\sffamily
    ]
    \node[Node=44mm, Pattern=red!30]     {desired pattern color: red};
    \node[Node=44mm, Pattern=cyan!30]      {desired pattern color: cyan};
    \node[Node=44mm]                    {without pattern};
    \node[Node=44mm, Pattern=orange!30]     {desired pattern color: orange};
  \end{tikzpicture}
\end{document}

色彩缤纷的图案

相关内容