PGF/TikZ에 현재 색상을 저장하는 방법은 무엇입니까?

PGF/TikZ에 현재 색상을 저장하는 방법은 무엇입니까?

TikZ/PGF에서는 를 사용하여 현재 선 너비를 얻을 수 있습니다 \pgflinewidth. 불행히도 (내가 아는 한) \pgfstrokecolor, \pgffillcolor, \pgfstrokeopacity및 은 없습니다 \pgffillopacity.

내 주요 질문은: 현재 그리기 및 채우기 색상을 복구할 수 있는 방법이 있습니까?

나는 마지막 색상이\color 복구 가능을 사용하면 \colorlet{saved}{.}점은 마지막 색상을 나타내기 때문입니다. 그리고 이는 \draw[red] ...(코드 참조) 처럼 색상을 스타일로 직접 설정할 때 사용할 수 있습니다 .

해킹을 시도했고 마지막 통화를 저장하는 방법도 있었지만 성공하지 못했습니다 \pgfsetstrokecolor.\pgfsetfillcolor\pgfsetcolor

다음 코드에서는 다른 레이어에서 재사용할 수 있도록 색상을 저장하는 몇 가지 트릭을 찾고 있습니다.

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

여기에 이미지 설명을 입력하세요

참고 1:다음 코드에서는 를 사용한 후 선 너비가 재설정되는 것을 볼 수 있습니다 pgfonlayer. 이것은 버그입니까, 아니면 (문서화된) 기능입니까?

노트 2:이상한 방식으로 텍스트 색상은 레이어 변경 후에도 유지되지만 다른 색상이나 선 너비는 유지되지 않습니다.

노트 3:\pgfsetcolorTikZ에서는 사용되지 않고 대신 \color사용된다는 인상을 받았습니다 . 그렇다면 왜 그렇습니까?


편집하다: 위의 코드는 단지 실제 예시일 뿐입니다. 색상을 복구하지 않고 이 특정 사례를 해결하고 싶지 않습니다.

답변1

이를 수행하는 개념적인 방법은 다음과 같습니다. 개선해야 할 부분이 많다고 생각하는데(색상 설명의 마지막 두 항목만 필요하기 때문에 매크로가 적고, 적절한 확장 제어, 사용자 정의 매크로 이름 가능성 등) 시간이 없었습니다. 여기에서 가져가셔도 될 것 같아요.

\documentclass[varwidth,border=50]{standalone}
\usepackage{tikz}
\makeatletter
\tikzset{get stroke color/.code={%
    \expandafter\global% Jump over, now we have \global
    \expandafter\let% Jump over now we have \global\let
    \expandafter\pgfsavedstrokecolor% Jump we have \global\let\pgf...
    \csname\string\color@pgfstrokecolor\endcsname% Finally expand this and put it at the end 
    },                                           % \global\let\pgf...{} in expanded form 
    restore stroke color/.code={\pgf@setstrokecolor#1},
}


\begin{document}
  \begin{tikzpicture}[green, draw=yellow!20!blue, ultra thick, fill=yellow, text=purple]
    \tikzset{get stroke color}% <-- Gets the drive-dependent low-level desc of y!20!b
    \show\pgfsavedstrokecolor% <-- Look at the log
    \filldraw (0,0) rectangle +(1,1) node[midway]{A};
    \pgfsetstrokecolor{orange} % <-- Change the drawing color to orange
    \draw (0.5,0.5) rectangle +(1,1) node[midway]{B}; % <-- Draw something to verify
    \tikzset{restore stroke color/.expand once=\pgfsavedstrokecolor} % <-- Restore it back
    \draw (1,1) rectangle +(1,1) node[midway]{C}; % <-- Verify
  \end{tikzpicture}
\end{document}

여기에 이미지 설명을 입력하세요

답변2

간단한 방법은 선언적 방법입니다. 사용 pgfkeys및 해당 .style핸들러를 사용하여 기본 설정(색상, 선 너비 등)을 저장하고 이를 여러 번 적용합니다.

여기에 이미지 설명을 입력하세요

\documentclass[varwidth,border=50]{standalone}
\usepackage{tikz}

\pgfdeclarelayer{foreground}
\pgfsetlayers{main,foreground}

\begin{document}
\tikzset{my preferecences/.style={green, draw=red, ultra thick,fill=yellow, text=purple}}
\begin{tikzpicture}[my preferecences]
    \filldraw (-.5,-.5) rectangle +(1,1) node[midway]{A};
    \begin{pgfonlayer}{foreground}
      \begin{scope}[my preferecences]
      \draw (0,0) rectangle +(1,1) node[midway]{B};
      \draw (.5,.5) rectangle +(1,1) node[midway]{C};
      \filldraw (1,1) rectangle +(1,1) node[midway]{D};
      \end{scope}
    \end{pgfonlayer}

    \filldraw (1.5,1.5) rectangle +(1,1) node[midway]{E}; % <- line width reset after pgfonlayer !
  \end{tikzpicture}
\end{document}

관련 정보