[Tex/LaTex] Gradient color on TikZ tree

node-connectionsnodestikz-pgftikz-trees

Mindmap creates connection line composed of the parent/child colors. Is it possible to apply such effect on normal parent/child trees to connect the parent and child nodes with a gradient of their corresponding colors? In a basic example of

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,trees}
\begin{document}

\begin{tikzpicture}
\node[circle,fill=blue] (par) at (0,0) {Parent}
    [level distance=4cm,sibling angle=25,clockwise from=80]
    child {node[circle,fill=red] (child1) {Child 1}}
    child {node[circle,fill=yellow] (child2) {Child 2}}
    child {node[circle,fill=green] (child3) {Child 3}};
\end{tikzpicture}

\end{document}

enter image description here

How to make a gradient of blue … red for the line connecting the parent to the first child, blue … yellow for the second child, etc.

Best Answer

I don't know how to do this with a tree and child but it's possible like this :

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,trees}
\usetikzlibrary{decorations}
\begin{document}

\makeatletter

\pgfkeys{/pgf/decoration/.cd,
         start color/.store in =\startcolor,
         end color/.store in   =\endcolor
}

\pgfdeclaredecoration{color change}{initial}{
 \state{initial}[width=0pt, next state=line, persistent precomputation={%
   \pgfmathdivide{50}{\pgfdecoratedpathlength}%
   \let\increment=\pgfmathresult%
   \def\x{0}%
 }]{}
 \state{line}[width=.5pt,   persistent postcomputation={%
     \pgfmathadd@{\x}{\increment}%
     \let\x=\pgfmathresult%
   }]{%
   \pgfsetarrows{-}%
   \pgfpathmoveto{\pgfpointorigin}%
   \pgfpathlineto{\pgfqpoint{.75pt}{0pt}}%
   \pgfsetstrokecolor{\endcolor!\x!\startcolor}%
   \pgfusepath{stroke}%
 }
 \state{final}{%
   \pgfpathmoveto{\pgfpointorigin}%
   \color{\endcolor!\x!\startcolor}%
   \pgfusepath{stroke}% 
 }}
\makeatother

\begin{tikzpicture}[every node/.style={circle,minimum width=1cm,font=\small},line width=1.6pt] 
  \node [fill=blue] (P) {Parent};
  \node [fill=red]    (C1) at (0:3)  {Child 1};
  \node [fill=yellow] (C2) at (30:3) {Child 2}; 
  \node [fill=green]  (C3) at (60:3) {Child 3};  
    \draw[line width=2pt, 
          decoration={color change, start color=blue, end color=red},
          decorate] (P) -- (C1) ;
\draw[line width = 2pt, 
      decoration = {color change, start color=blue, end color=yellow}, 
          decorate] (P) -- (C2) ;   
\draw[line width = 2pt, 
      decoration = {color change, start color=blue, end color=green}, 
          decorate] (P) -- (C3) ;   
\end{tikzpicture}

\end{document} 

enter image description here