是否可以获取节点的颜色?

是否可以获取节点的颜色?

如何获取节点的颜色?

客观的: 在此处输入图片描述

我迄今为止的代码:

\documentclass[border=2mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc} % calculates coordinates
\usetikzlibrary{ backgrounds } 
\usetikzlibrary{ graphs, graphs.standard }
\usetikzlibrary{ positioning }
\usetikzlibrary{ intersections }

\usepackage{xparse} %For NewDocumentCommand

\usepackage{xcolor}

\tikzset{ every path/.style = {
        line width = 0.5mm
    }
}

\tikzset { myPlainVrtxStyle/.style = {
        circle, minimum size= 5mm,
        draw= #1!55!black!90,
        fill = #1,
    }
}

\makeatletter
\tikzset { myVSplitPlainVrtxStyle/.style  args={#1,#2}{%
        circle,
        minimum size= 5mm,
        draw= #1!55!black!90,
        fill = #1,
        alias=tmp@name,
        postaction={%
            insert path={
                \pgfextra{% 
                    \pgfpointdiff{\pgfpointanchor{\pgf@node@name}{center}}%
                    {\pgfpointanchor{\pgf@node@name}{east}}%            
                    \pgfmathsetmacro\insiderad{\pgf@x}
                    \fill[#2] (\pgf@[email protected]) ([yshift=\pgflinewidth]\pgf@[email protected])  arc (-90:90:\insiderad-\pgflinewidth)--cycle;
                    \draw[#2!55!black!90] (\pgf@[email protected]) ([yshift=\pgflinewidth/2]\pgf@[email protected])  arc (-90:90:\insiderad-\pgflinewidth/2); 
                }
            }
        }
    }
}
\makeatother 

%Predefined: black, blue, brown, cyan, darkgray, gray, green, lightgray, lime, magenta, olive, orange, pink, purple, red, teal, violet, white, yellow. 

\NewDocumentCommand{\leftG}{m}{
    \def \angleOfExit {10}
    \node[myPlainVrtxStyle = blue] (0) at #1 {};
    \node[myPlainVrtxStyle = teal] at ($ (0) + (0, -2) $) (1) {};
    \node[myPlainVrtxStyle = cyan] at ($ (1) + (-90 + \angleOfExit : 2cm) $) (2) {};
    \node[myPlainVrtxStyle = violet] at ($ (1) + (-90 - \angleOfExit : 3.5cm) $) (3) {};
    
    \path[name path = intersectionLine1] (3) -- +(-90 + \angleOfExit : 10cm); 
    \path[name path = intersectionLine2] (2) -- +(-90 - \angleOfExit : 10cm); 
    \path[name intersections = {of = intersectionLine1 and intersectionLine2}];
    \node[myPlainVrtxStyle = olive] at (intersection-1) (4) {};
    
    \graph[ use existing nodes ]{
        0 -- 1 -- {2, 3} -- 4;
    };
}

\NewDocumentCommand{\rightG}{m}{
    \def \angleOfExit {10}
    \node[myPlainVrtxStyle = lime] (0) at #1 {};
    \node[myPlainVrtxStyle = orange] at ($ (0) + (2cm, 0) $) (1) {};
    \node[myPlainVrtxStyle = brown] at ($ (1) + (-\angleOfExit : 2cm) $) (2) {};
    \node[myPlainVrtxStyle = black] at ($ (1) + (\angleOfExit : 3.5cm) $) (3) {};
    
    \graph[ use existing nodes ]{
        0 -- 1 -- {2, 3}
    };
}

\begin{document}
    
    \begin{tikzpicture}
        \begin{scope}[ name prefix = LG- ]
            \leftG{(0,0)};
        \end{scope}
        
        \path let \p1 = ($(LG-0) - (LG-4)$),
        \p2 = (LG-2),
        in
        node (crossPosition) at ($ (\x2 + 1.5cm, -\y1/2) $) {\Huge$\times$};
        
        
        \begin{scope}[ name prefix = RG- ]
            \rightG{($ (crossPosition) + (1.5cm, 0) $)}
        \end{scope}
        
        \path let \p1 = (crossPosition),
        \p2 = (RG-3),   
        in
        node (eqPosition) at ($ (\x2 + 1.5cm, \y1) $) {\Huge$=$};
        
        \foreach \lv in {0, ..., 4}{ %For each v in the lG
            \path let \p1 = (LG-\lv),
            \p2 = (eqPosition),
            in 
            coordinate (copyRGLeft\lv) at ( \x2 + 1.5cm + \x1, \y1);
            
            \begin{scope}[ name prefix = CP-\lv ]
                \rightG{ (copyRGLeft\lv) };
            \end{scope}
        }
        
        \foreach \rv in {0, ..., 3} { %For each v in the rG
            \graph[ use existing nodes ]{
                (CP-0\rv) -- (CP-1\rv) -- { (CP-2\rv), (CP-3\rv) } -- (CP-4\rv);
            };
        }
        
        % Changing the colors
        \begin{pgfonlayer}{main}
            \node[ myVSplitPlainVrtxStyle = {blue, lime} ] at (CP-00) {}; %TODO
        \end{pgfonlayer}
        
    \end{tikzpicture}
    
\end{document}

在此处输入图片描述

我认为,如果我可以根据节点 ID 获取节点的颜色,那么我稍后需要做的“分割着色”就会变得简单得多。这可能吗?

简而言之,我正在研究定义获取颜色在下面的代码中:

\begin{pgfonlayer}{main}
  \foreach \lv in {0, ..., 4}
     \foreach \rv in {0, ..., 3}
        \node[ myVSplitPlainVrtxStyle = { getColor{LG-\lv}, getColor{RG-\rv} } ] at (CP-\lv\rv) {};
\end{pgfonlayer}

欢迎任何方面的改进或想法。

编辑:在@Black Mild 回答之后,我修改了问题以消除任何歧义。

答案1

以下代码添加了一个例程,用于记住每个节点的绘制颜色和填充颜色。如果节点名为Alice,则绘制颜色为Alice.d,填充颜色为Alice.f

\documentclass{article}

\usepackage{tikz}
\makeatletter
\def\pgf@sh@fbg@circle{%
  \@ifundefinedcolor{pgffillcolor}{}{\xglobal\colorlet{\pgf@[email protected]}{pgffillcolor}}%
  \@ifundefinedcolor{pgfstrokecolor}{}{\xglobal\colorlet{\pgf@[email protected]}{pgfstrokecolor}}%
}
\def\pgf@sh@fbg@rectangle{%
  \@ifundefinedcolor{pgffillcolor}{}{\xglobal\colorlet{\pgf@[email protected]}{pgffillcolor}}%
  \@ifundefinedcolor{pgfstrokecolor}{}{\xglobal\colorlet{\pgf@[email protected]}{pgfstrokecolor}}%
}
\makeatother

\begin{document}

    \tikz\node(grape)[circle,draw=red!60!blue,fill=red!50!blue!40]{grape};
    \tikz\node(orange)[draw=red!55!yellow,fill=red!65!yellow!45]{orange};
    
    One hundred years later...
    
    \tikz\node[draw=grape.d,fill=grape.f]{remember grape?};
    
    One thousand years later...
    
    \tikz\node[circle,draw=orange.f,fill=orange.d]{remember orange?};

\end{document}

请注意:

  • circle它只记住和节点的颜色rectangle。对于其他形状,请复制代码并修改\def\pgf@sh@fbg@*circle*

  • 此 hack 已插入\beforebackgroundpath例程中。如果您需要此例程,请将 hack 插入其他地方。(如果您不知道,\beforebackgroundpath那么您就安全了。)

  • \tikz@fillcolorTikZ 还保留了、\tikz@strokecolor和中颜色的副本 \tikz@textcolor

答案2

我有一个建议,node在里面 使用一个圆圈pic,里面有 3 个参数:#1是左边的颜色,#2是右边的颜色,#3是覆盖节点的名称。所以我们结合了node(命名,以后再引用)的良好特征和pic(自由形状)的良好特征。

在此处输入图片描述

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\tikzset{pics/colorpic/.style args=
{left #1 right#2 nodename #3}{code={%
\fill[color=#1] (90:.2) arc(90:270:.2);
\fill[color=#2] (90:.2) arc(90:-90:.2);
\path (0,0) node[circle,draw,minimum size=4mm] (#3) {};%
}}} 
    
\path
(0,0) pic{colorpic=left red right blue nodename A}
(2,1) pic{colorpic=left cyan right magenta nodename B}
;
\draw (A)--(B);
\draw[-stealth] (A.120) to[out=150,in=80] (B.north);
\end{tikzpicture}
\end{document}  

更新这是 OP 图形的自动解决方案。

在此处输入图片描述

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[thick]
\tikzset{n/.style={circle,draw,minimum size=4mm},
pics/colordisk/.style args={left #1 right#2 nodename #3}{code={%
\fill[#1] (90:.2) arc(90:270:.2);
\fill[#2] (90:.2) arc(90:-90:.2);
\path (0,0) node[n] (#3) {};%
}}} 
        
% colors for the vertical nodes Ai
\colorlet{mauA1}{blue}
\colorlet{mauA2}{cyan}
\colorlet{mauA3}{teal}
\colorlet{mauA4}{teal!50}
\colorlet{mauA5}{green}
        
% colors for the horizontal nodes Bj
\colorlet{mauB1}{red}
\colorlet{mauB2}{orange}
\colorlet{mauB3}{yellow}
\colorlet{mauB4}{pink}
        
% draw vertical nodes A\i
\draw 
(-1.2,2)  node[n,fill=mauA1] (A1) {}--
(-1.2,1)  node[n,fill=mauA2] (A2) {}--
(-1,0)    node[n,fill=mauA3] (A3) {}--
(-1.2,-2) node[n,fill=mauA4] (A4) {}--
(-1.4,-1) node[n,fill=mauA5] (A5) {}--(A2)
;
        
% draw horizontal nodes B\j
\draw 
(0,0)   node[n,fill=mauB1] (B1) {}--
(1,0)   node[n,fill=mauB2] (B2) {}--
(2,-.3) node[n,fill=mauB3] (B3) {}
(3,.3)  node[n,fill=mauB4] (B4) {}--(B2)
;
        
% generating new node C\i\j by mixing colors via `pic`
\foreach \i in {1,...,5}{
\foreach \j in {1,...,4}
\path (A\i.center)++(B\j.center)+(5.5,0) 
pic{colordisk=left {mauA\i} right {mauB\j} nodename C\i\j};
\draw (C\i1)--(C\i2)--(C\i3) (C\i2)--(C\i4);
}
;
        
% connecting nodes C\i\j
\foreach \j in {1,...,4}
\draw (C1\j)--(C2\j)--(C3\j)--(C4\j)--(C5\j)--(C2\j);

\path[nodes={scale=1.2,gray}]
(-.5,0) node{$\times$} (3.75,0) node{=};
\end{tikzpicture}
\end{document}

相关内容