pgf 基本层:再次与颜色斗争

pgf 基本层:再次与颜色斗争

这个问题与以下问题相关(但并不相同):另一个

我正在尝试使颜色circuitikz更合理地使用。现在我正在努力在低水平上管理颜色。在我的形状中,我需要用指定的fill颜色填充部分,而其他部分则应始终用描边(或)颜色填充draw。我天真地尝试了以下方法:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\pgfdeclareshape{sline}{
    \anchor{center}{\pgfpointorigin}%
    \behindbackgroundpath{
        \pgfpathmoveto{\pgfpoint{-0.3cm}{0pt}}%
        \pgfpathlineto{\pgfpoint{0.3cm}{0pt}}%
        \pgfusepath{draw}%
        % lower rectangle
        \pgfpathrectanglecorners{\pgfpoint{-0.3cm}{-4pt}}{\pgfpoint{0.3cm}{-8pt}}
        \pgfusepath{fill, draw}
        % upper rectangle
        \pgfpathrectanglecorners{\pgfpoint{-0.3cm}{4pt}}{\pgfpoint{0.3cm}{8pt}}
        % \pgfsetfillcolor{cyan}% <- works, always cyan
        \pgfsetfillcolor{pgfstrokecolor}% I expected a red color...
        \pgfusepath{draw, fill}
    }
}
\begin{document}
\color{blue} Text
\begin{tikzpicture}[]
    \draw[] (0,1)  node [sline]{} (1,1) node{aaa};
    \draw[red, fill=green] (0,0)  node [sline]{} (1,0) node{bbb};
\end{tikzpicture}
\end{document}

失败的原因如下:

在此处输入图片描述

所以我又回到这里:有一种方法可以说:好的,从现在开始让填充颜色等于绘制(描边)颜色?这意味着在上面的例子中,上面的矩形应该用红色填充,或者用\draw-> 中指定的任何颜色填充,就像“bbb”文本所做的那样。

###更新

感谢 Ulrike Fischer,我在这里得到了更多数据。看起来简单的red(与 相同)color=red填充了\tikz@textcolor宏,但是不是 \tikz@strokecolor,为空。如果指定,draw=red则相反。奇怪的是,如果您说redcolor=red,则描边颜色红色作为文本颜色,所以我希望它填充两者。

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\makeatletter
\pgfdeclareshape{sline}{
    \anchor{center}{\pgfpointorigin}%
    \behindbackgroundpath{
        \pgfpathmoveto{\pgfpoint{-0.3cm}{0pt}}%
        \pgfpathlineto{\pgfpoint{0.3cm}{0pt}}%
        \pgfusepath{draw}%
        % lower rectangle
        \pgfpathrectanglecorners{\pgfpoint{-0.3cm}{-4pt}}{\pgfpoint{0.3cm}{-8pt}}
        \pgfusepath{fill, draw}
        % upper rectangle
        \pgfpathrectanglecorners{\pgfpoint{-0.3cm}{4pt}}{\pgfpoint{0.3cm}{8pt}}
        %
        % \pgfsetfillcolor{cyan}% <- works, always cyan
        % \pgfsetfillcolor{pgfstrokecolor}% I expected a red color...
        % \pgfsetfillcolor{\tikz@strokecolor}
        \pgfcircsetfillcolorifnotempty{\tikz@strokecolor}
        % \ifdefempty\tikz@strokecolor{}{\pgfsetfillcolor{\tikz@strokecolor}}
        \typeout{COLORstroke \tikz@strokecolor}
        \typeout{COLORtext \tikz@textcolor}
        \typeout{}
        \pgfusepath{draw, fill}
    }
}
\def\pgfcircsetfillcolorifnotempty#1{%
    \ifx#1\pgfutil@empty
    \else
        \pgfsetfillcolor{#1}%
    \fi
}

\makeatother
\begin{document}
\color{blue} Text
\begin{tikzpicture}[]
    \draw[] (0,1)  node [sline]{} ++(1,0) node[draw,fill]{aaa};
    \draw[red, fill=green] (0,0)  node [sline]{} ++(1,0) node[draw,fill]{bbb};
    \draw[draw=red, fill=green] (0,-1)  node [sline]{} ++(1,0) node[draw,fill]{bbb};
    \draw[color=red, fill=green] (0,-2)  node [sline]{} ++(1,0) node[draw,fill]{bbb};
\end{tikzpicture}
\end{document}

在此处输入图片描述

上面写着:

COLORstroke 
COLORtext 

COLORstroke 
COLORtext red

COLORstroke red
COLORtext 

COLORstroke 
COLORtext red

所以也许我有一个解决方案......

答案1

感谢评论区用户的帮助——Ulrike Fischer 提供了探索事物的工具, 和muzimuzhi Z 对此回答以及向我展示 Ti 的内部结构Z 颜色,我设法找到了一个(部分但仍然)解决方案。

如果用户draw在中途更改颜色,则此方法会失败,但我认为这是目前最好的选择。此外,如果在中途更改颜色,则方法会失败,如上例所示 --- 这有点可惜;如果全部为sline红色(从某种意义上说,颜色是构建时的颜色),behindbackgroundpath我会更高兴,但是...)。

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{circuitikz}
\usepackage{regexpatch} % for the starred-form of \xpatchcmd
\makeatletter
\let\ctikz@strokecolor\pgfutil@empty
\pgfkeys{/tikz/color/.add code={}{\edef\ctikz@strokecolor{#1}}}
% note `/tikz/.unknown/.@body` is not updated
\pgfkeysgetvalue{/tikz/.unknown/.@cmd}{\my@temp}
\xpatchcmd*\my@temp % use starred-form to replace all (two places actually)
  {\expandafter\tikz@addoption\expandafter}
    {\edef\ctikz@strokecolor{\tikz@key}%
    \expandafter\tikz@addoption\expandafter}
  {}{%
  \pgfutil@packagewarning{circuitikz}{%
    Color patch failed, expect strange colors (see manual)}}
\pgfkeyslet{/tikz/.unknown/.@cmd}{\my@temp}
%
\pgfdeclareshape{sline}{
    \anchor{center}{\pgfpointorigin}%
    \behindbackgroundpath{
        \pgfpathmoveto{\pgfpoint{-0.3cm}{0pt}}%
        \pgfpathlineto{\pgfpoint{0.3cm}{0pt}}%
        \pgfusepath{draw}%
        % lower rectangle
        \pgfpathrectanglecorners{\pgfpoint{-0.3cm}{-4pt}}{\pgfpoint{0.3cm}{-8pt}}
        \pgfusepath{fill, draw}
        % upper rectangle
        \pgfpathrectanglecorners{\pgfpoint{-0.3cm}{4pt}}{\pgfpoint{0.3cm}{8pt}}
        %
        % \pgfsetfillcolor{cyan}% <- works, always cyan
        \pgf@circ@setfillcolorasdraw
        \pgfusepath{draw, fill}
        \pgfsetcolor{black}
        \pgftext[y=10pt]{\tiny S:\tikz@strokecolor~T:\tikz@textcolor~F:\tikz@fillcolor}
        \pgftext[y=16pt]{\tiny CS:\ctikz@strokecolor}
    }
}
\def\pgf@circ@setfillcolorasdraw{%
    \ifx\tikz@strokecolor\pgfutil@empty
        \ifx\ctikz@strokecolor\pgfutil@empty
            \pgfsetfillcolor{.}
        \else
            \pgfsetfillcolor{\ctikz@strokecolor}
        \fi
    \else
        % we have strokecolor, yay!
        \pgfsetfillcolor{\tikz@strokecolor}
    \fi
}
\makeatother
\begin{document}

\color{blue} Text default is blue
\begin{tikzpicture}[baseline=2cm]
    \draw[] (0,2) node [sline]{} ++(1,0) node[draw,fill]{A} ++(1,0) node[right]{no options};
    \draw[red] (0,1) node [sline]{} ++(1,0) node[draw,fill]{B} ++(1,0) node[right]{red};
    \draw[red, fill=green] (0,0) node [sline]{} ++(1,0) node[draw,fill]{C} ++(1,0) node[right]{red, fill=green};
    \draw[color=red, fill=green] (0,-1) node [sline]{} ++(1,0) node[draw,fill]{D} ++(1,0) node[right]{color=red, fill=green};
    \draw[draw=red, fill=green] (0,-2) node [sline]{} ++(1,0) node[draw,fill]{E} ++(1,0) node[right]{draw=red, fill=green};
    \draw[text=red, fill=green] (0,-3) node [sline]{} ++(1,0) node[draw,fill]{F} ++(1,0) node[right]{text=red, fill=green};
    \draw[red] (0,-4) node [sline]{} ++(1,0) [black] node[draw,fill]{F} ++(1,0) node[right]{red, then black};
\end{tikzpicture}

\end{document}

在此处输入图片描述

现在我的问题是,我不确定这是否是对之前状态的增强circuitikz...我会考虑一下。

相关内容