# Gradient color on TikZ tree

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}


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.

-
I think for this purpose you could adopt the Altermundus' approach in this answer. –  Claudio Fiandrino Jun 4 '12 at 18:30

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={%
\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}


-
This works well, but the only problem is that the start-end colors should be defined manually, and it will not detect them from the node colors. Anyway, it is not a normal tree, and lines should be drawn individually. –  All Jun 4 '12 at 19:43
Yes you need to add color manually but I don't know how to use the color of the nodes. –  Alain Matthes Jun 4 '12 at 19:54
Maybe make the connecting lines a little thicker? Like 1/3 the diameter of the circles? –  Todd Lehman Jun 4 '12 at 20:32
This is awesome. Can the color change decoration be made to work with arrows? Right now, a final arrowhead can point in the wrong direction when using -| and -| to construct paths. –  Neil G Jun 20 '12 at 2:13