5

How can I obtain the color of a node?

Objective: enter image description here

My code so far:

\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@node@name.base) ([yshift=\pgflinewidth]\pgf@node@name.south) arc (-90:90:\insiderad-\pgflinewidth)--cycle; \draw[#2!55!black!90] (\pgf@node@name.base) ([yshift=\pgflinewidth/2]\pgf@node@name.south) 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}

enter image description here

I think if I can obtain the color of the nodes based on their ID the "split coloring" that I need to do later becomes much simpler. Is this possible?

In short, I am investigating the possibility of defining getColor in the following code:

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

Any improvements or idea in any aspect is welcomed.

Edit: After the answer of @Black Mild I have modified the question to eliminate any ambiguity.

Aria
  • 393
  • I don't think it's possible... I suggest using an array of colors so that you can reuse them by index. https://tex.stackexchange.com/a/45059/38080 – Rmano Jun 21 '21 at 06:15
  • @Rmano Would you mind providing the syntax for that, if it is easy? I can search, but I am being lazy. :) – Aria Jun 21 '21 at 06:40
  • I Will try when I have a little time... let's see if somebody gets the idea. You could help by reducing the MWE to just three nodes or something like that... – Rmano Jun 21 '21 at 08:53
  • @Aria May I ask where does the figure come from ? or the context of the figure? – Black Mild Jun 22 '21 at 05:02
  • 1
    aha, that is Cartesian product of graphs https://en.wikipedia.org/wiki/Graph_product – Black Mild Jun 22 '21 at 06:06
  • @Black Mild, Yes, correct. – Aria Jun 23 '21 at 03:37

2 Answers2

8

I have a suggestion for you, using a circle node inside pic with 3 arguments: #1 is the color for the left, #2 is the color for the right, #3 is the name of covering node. So we combine good features of node (to name it, to refer it later) and good features of pic (free shape).

enter image description here

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

Update This is an automatic solution for OP's figure.

enter image description here

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

Black Mild
  • 17,569
8

The following code adds a routine that remembers every node's draw color and fill color. If the node is named Alice, the draw color is Alice.d and the fill color is Alice.f

\documentclass{article}

\usepackage{tikz} \makeatletter \def\pgf@sh@fbg@circle{% @ifundefinedcolor{pgffillcolor}{}{\xglobal\colorlet{\pgf@node@name.f}{pgffillcolor}}% @ifundefinedcolor{pgfstrokecolor}{}{\xglobal\colorlet{\pgf@node@name.d}{pgfstrokecolor}}% } \def\pgf@sh@fbg@rectangle{% @ifundefinedcolor{pgffillcolor}{}{\xglobal\colorlet{\pgf@node@name.f}{pgffillcolor}}% @ifundefinedcolor{pgfstrokecolor}{}{\xglobal\colorlet{\pgf@node@name.d}{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}

Please note that:

  • It only remembers the color of circle and rectangle nodes. For other shapes, duplicate the code and modify \def\pgf@sh@fbg@*circle*.

  • This hack is inserted in the \beforebackgroundpath routine. If you need this routine, insert the hack elsewhere. (If you don't know \beforebackgroundpath then you are safe.)

  • TikZ also keeps a copy of colors in \tikz@fillcolor, \tikz@strokecolor, and \tikz@textcolor.

Symbol 1
  • 36,855