In TikZ/PGF, we can obtain the current line width using \pgflinewidth. Unfortunatelly there is no (as far as I know) \pgfstrokecolor, \pgffillcolor, \pgfstrokeopacity and \pgffillopacity.
My main question is : Is there some way to recover the current draw and fill color ?
I know that the last color set by \color can be recovered with \colorlet{saved}{.}, because dot represent the last color. And this can be used when we directly set the color as a style, like in \draw[red] ... (see the code).
I've tried to hack \pgfsetstrokecolor, \pgfsetfillcolor and \pgfsetcolor in a way to save the last call ... but without success.
In the following code I'm looking for some trick to save the colors in a way to be able to reuse them in another layer.
\documentclass[varwidth,border=50]{standalone}
\usepackage{tikz}
\pgfdeclarelayer{foreground}
\pgfsetlayers{main,foreground}
\begin{document}
\begin{tikzpicture}[green, draw=red, ultra thick, fill=yellow, text=purple]
\filldraw (-.5,-.5) rectangle +(1,1) node[midway]{A};
\pgfmathsetlengthmacro{\savedpgflinewidth}{\pgflinewidth}
\colorlet{saved}{.}
\def\pgfstrokecolor{red} % <- to be replaced by \strokecolorlet
\def\pgffillcolor{yellow} % <- to be replaced by \fillcolorlet
\begin{pgfonlayer}{foreground}
\draw (0,0) rectangle +(1,1) node[midway]{B};
\color{saved} % <- restore the color set by \color{green}
\pgfsetlinewidth{\savedpgflinewidth} % <- restore the line width
\draw (.5,.5) rectangle +(1,1) node[midway]{C};
\pgfsetstrokecolor{\pgfstrokecolor} % <- restore the draw color
\pgfsetfillcolor{\pgffillcolor} % <- restore the fill color
\filldraw (1,1) rectangle +(1,1) node[midway]{D};
\end{pgfonlayer}
\filldraw (1.5,1.5) rectangle +(1,1) node[midway]{E}; % <- line width reset after pgfonlayer !
\end{tikzpicture}
\end{document}

Note 1: In the following code we can see that the line width is reset after the use of pgfonlayer. Is this a bug or a (documented) feature ?
Note 2: In a strange way the text color survive the layer change, but not the other colors, neither the line width.
Note 3: I have the impression that \pgfsetcolor is not used by TikZ, instead \color is used. If this is the case, why ?
EDIT : The code above is just a working example. I don't want to solve this particular case without recovering the colors.


pgfkeysand its.stylehandler to store your preferences (color, line width, etc.) and to apply them multiple times. – Paul Gaborit Dec 21 '14 at 17:49\color@pgfstrokecolor– percusse Dec 21 '14 at 19:51pgfcoregraphicstate.code.texand it is used always inside a long long sequence of\expandafterthat I can't understand ;) – Kpym Dec 21 '14 at 21:31