TikZ/Forest: How to uniquely color parent/children/grandchildren branch nodes?

You might use the following style branch shade=from <colour> to <colour> to shade the branches as follows.

[Edited to support colours such as blue!80 which leave the specification of white as the second colour implicit`.]

The new version of the code also creates a family tree style for trees which should follow this structural pattern. twist=<integer> sets the level at which the change should take place, so that children in later levels will be set folder style i.e. directory-like. The default is 2.

As Alan Munn indicated, this is really the same as before (which also implicitly relied on a nodewalk via, for example, the .max handler).

Note that

l sep+=0pt,

has no effect on anything whatsoever. It adds a length of zero to a dimension, which is obviously equivalent to nothing at all.

Then we can write

\begin{forest}
  family tree,
   [Grandparent, left color=cyan, right color=SpringGreen, middle color=Pink, draw=Silver
     [Parent 1, branch shade=from cyan to blue
       [Child 1
         [Grandchild 1]
       ]
     ]
     [Parent 2, branch shade=from Pink to WildStrawberry
       [Child 2
         [Grandchild 2]
       ]
     ]
     [Parent 3, branch shade=from SpringGreen to ForestGreen
       [Child 3
         [Grandchild 3]
       ]
     ]
   ]
\end{forest}

to produce the tree created by the version of the code in the original version of this answer.

fancy-coloured tree

As you can see, I had no idea what to do with the root.

Or we can write

\begin{forest}
  family tree,
   [Grandparent, fill=darkgray, text=Silver, double=Silver, draw=darkgray
     [Parent 1, branch shade=from blue!80 to blue!20
       [Child 1
         [Grandchild 1]
       ]
     ]
     [Parent 2, branch shade=from WildStrawberry!80 to WildStrawberry!20
       [Child 2
         [Grandchild 2]
       ]
     ]
     [Parent 3, branch shade=from ForestGreen!80 to ForestGreen!20
       [Child 3
         [Grandchild 3]
       ]
     ]
   ]
\end{forest}

to make use of white implicitly and style the root node differently.

implicit use of white & plainer root

Complete code:

\documentclass[border=10pt,multi,tikz,dvipsnames,svgnames,rgb]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows.blur}
\forestset{
  declare dimen register=gap,
  gap'=10mm,
  declare count register=twist,
  twist'=2,
  family tree/.style={
    forked edges,
    for tree={
      rounded corners,
      minimum width/.wrap pgfmath arg={##1}{(\textwidth-6*(gap))/3},
      minimum height=4ex,
      edge={-Latex},
      font=\sffamily,
      text centered,
      blur shadow,
      edge=thick,
    },
    where={level()<(twist)}{%
      parent anchor=children,
      l sep+=10pt,
      s sep'+=10pt,
    }{%
      folder,
      grow'=0,
      l sep'+=2pt,
      if={level()==(twist)}{%
        before typesetting nodes={child anchor=north},
        !u.s sep'+=10pt,
        edge path'={%
          (!u.parent anchor) -- ++(0,-10pt) -| (.child anchor)
        },
      }{},
    },
  },
  branch shade/.style args={from #1 to #2}{
    before typesetting nodes={
      tempcountc/.max={level}{current,tree},
      tempcountb/.option=level,
      tempcounta=(tempcountc)-(tempcountb)+1,
      temptoksa/.option=name,
      TeX/.wrap pgfmath arg={
        \colorlet{##1col1}{#1}
        \colorlet{##1col2}{#2}
      }{name()},
      for tree={
        rounded corners,
        top color/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb))/(tempcounta))}{(temptoksa)},
        +edge/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb))/(tempcounta))}{(temptoksa)},
        bottom color/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb)+1)/(tempcounta))}{(temptoksa)},
        draw/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb)+1)/(tempcounta))}{(temptoksa)},
        thick,
      },
    }
  },
}
\begin{document}
\begin{forest}
  family tree,
   [Grandparent, left color=cyan, right color=SpringGreen, middle color=Pink, draw=Silver
     [Parent 1, branch shade=from cyan to blue
       [Child 1
         [Grandchild 1]
       ]
     ]
     [Parent 2, branch shade=from Pink to WildStrawberry
       [Child 2
         [Grandchild 2]
       ]
     ]
     [Parent 3, branch shade=from SpringGreen to ForestGreen
       [Child 3
         [Grandchild 3]
       ]
     ]
   ]
\end{forest}
\begin{forest}
  family tree,
   [Grandparent, fill=darkgray, text=Silver, double=Silver, draw=darkgray
     [Parent 1, branch shade=from blue!80 to blue!20
       [Child 1
         [Grandchild 1]
       ]
     ]
     [Parent 2, branch shade=from WildStrawberry!80 to WildStrawberry!20
       [Child 2
         [Grandchild 2]
       ]
     ]
     [Parent 3, branch shade=from ForestGreen!80 to ForestGreen!20
       [Child 3
         [Grandchild 3]
       ]
     ]
   ]
\end{forest}
\end{document}

Here's a different version (slightly less complicated):

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows.blur}
\begin{document}
\newlength\gap
\setlength\gap{10mm}
\forestset{parent color/.style args={#1}{
    {fill=#1},
    for tree={fill/.wrap pgfmath arg={#1!##1}{1/level()*80},draw=#1!80!darkgray}},
    root color/.style args={#1}{fill={{#1!60!gray!25},draw=#1!80!darkgray}}
}
\begin{forest}
  forked edges,
  for tree={%
    rounded corners,
    minimum width=(\textwidth-6*\gap)/3,
    minimum height=4ex,
    edge={-Latex},
    font=\sffamily,
    text centered,
    blur shadow,
  },
  where={level()<=1}{%
    parent anchor=children,
    l sep+=10pt,
    s sep'+=10pt,
   }{%
     folder,
     grow'=0,
     l sep+=2pt,% length of edge to grand child
     if level=2{%
       before typesetting nodes={child anchor=north},
       !u.s sep'+=10pt,
       edge path'={%
         (!u.parent anchor) -- ++(0,-10pt) -| (.child anchor)
       },
     }{},
   },
      [Grandparent,root color={brown}
        [Parent 1, parent color={red}
            [Child 1
                [Grandchild 1]
            ]
        ]
        [Parent 2,parent color={green}
            [Child 2
                [Grandchild 2]
            ]
        ]
        [Parent 3,parent color={yellow}
            [Child 3
                [Grandchild 3]
            ]
        ]
    ]
\end{forest}
\end{document}

output of code

Tags:

Forest