This question is linked (but not the same) to this other one.
I am trying to make the color in circuitikz more reasonable to use. Now I am struggling with managing colors at a low level. In my shapes, I need to fill parts of them with the specified fill color, and others that should always be filled with the stroke (or draw) color.
I naïvely tried this:
\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}
Which fails as:
So I am here again: there is a way to say: ok, from now on make the fill color equal to the draw (stroke) color? That means that in the above example, the upper rectangle should be filled in red, or in whatever color is specified in the \draw -> like the "bbb" text is doing.
###update
Thanks to Ulrike Fischer, I have more data here. It seems that the simple red, which is the same as color=red, fills the \tikz@textcolor macro but not \tikz@strokecolor, which is empty. If you specify draw=red then you have the other way around. What is strange, is that if you say red, or color=red, the stroke color is red as the text color, so I expected it filled both.
\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}
and it says:
COLORstroke
COLORtext
COLORstroke
COLORtext red
COLORstroke red
COLORtext
COLORstroke
COLORtext red
so maybe I have a solution...



\ifdefempty\tikz@strokecolor{}{\pgfsetfillcolor{\tikz@strokecolor}}– Ulrike Fischer Feb 23 '22 at 17:25\tikz@strokecoloris not defined at that point (nor\ifdefempty, really...) – Rmano Feb 24 '22 at 08:35\tikz@textcolor, which is filled with the draw color is there is no explicittext=key. I will study Steven's answer you pointed to, but it needs to actively store the color in the draw, it seems, and I'd like to have it transparently work. Yes, I noticed that usingdraw=redexplicitly works... which is strange,redshould be equivalent... – Rmano Feb 24 '22 at 08:50draw,fill, andcoloras path options. The reason is, code for these options are appended to\tikz@optionsand the latter is not executed until a tikz path ends (in\tikz@finish). But the code for node path is used (and saved in a box) when a node is encountered on a tikz path. The overall order is like\def\tikz@options{\pgfsetcolor{...}} \setbox\nodebox{<node path and text>} \tikz@options \box\nodebox. – muzimuzhi Z Feb 25 '22 at 03:53\path[<color>]is special because a\pgfutil@color{<color>}is executed immediately. Also value of\tikz@strokecoloris not reliable when anotherdraw=<color>is encountered on the same path and after the node. In\draw[draw=red] node[sline] {} [draw=cyan];thedraw=cyanwins but at the timenode[sline] {}is saved in a box, it has no info about the following path options. Since a rectangle is simple enough it's possible to do calculations and draw it as a thick line. Otherwise providing a new color option is easier. – muzimuzhi Z Feb 25 '22 at 03:56\tikz@strokecolor(and maybe fill) set incolor=redandred. – Rmano Feb 25 '22 at 08:49