As mentionned in Martin's comment, the trick is to draw the line twice. One of the constraints on paths in Tikz/PGF is that the color is global to the path.
Other than drawing a solid line covered by a dashed line, you may draw two dashed lines, with spaces between the dashes, as given by the following example (you can find more information in the Tikz manual) :
\begin{tikzpicture}
\draw[blue,dash pattern= on 3pt off 5pt] (0,0) |- (1,1) to[out=0,in=90] (2,0);
\draw[red,dash pattern= on 3pt off 5pt,dash phase=4pt] (0,0) |- (1,1) to[out=0,in=90] (2,0);
\end{tikzpicture}
The outcome is
![two color dashed line](https://i.stack.imgur.com/BqXxS.png)
(Following a comment by Caramdir) : The same result can be achieved with a postaction, and the path only has to be specified once :
\draw[postaction={draw,red,dash pattern= on 3pt off 5pt,dash phase=4pt,thick}]
[blue,dash pattern= on 3pt off 5pt,thick] (0,0) |- (1,1) to[out=0,in=90] (2,0);
(Following a request for a rectangle) * I'm not sure this answers the question in the comment *
For a rectangle you would type in the command
\draw[postaction={draw,red,dash pattern= on 3pt off 5pt,dash phase=4pt,thick}]
[blue,dash pattern= on 3pt off 5pt,thick] (0,0) rectangle (3,2);
This rectangle does not have rounded corners, it is a "normal" rectangle. If ever you do want rounded corners, add rounded corners
to the options :
\draw[postaction={draw,red,dash pattern= on 3pt off 5pt,dash phase=4pt,thick,rounded corners}]
[blue,dash pattern= on 3pt off 5pt,thick,rounded corners] (0,0) rectangle (3,2);
The problem with trying to do a "color-bar" style shading is that in the process of fitting a shading to a path PGF scales a shading so that only the center quarter of the shading is seen (see "Using Shadings" in the manual).
This means that either a color-bar has to be "squidged" into the center quarter of a shading definition, which is a pain to to manually, or the path to be shaded has to be clipped, and scaled manually which can also be a bit of a nuisance.
The following shows one way of specifying a color-bar with a list of named colors (assumed to occupy equal width of the bar) and automatically generate a (more-or-less) appropriate shading:
\documentclass[tikz, border=5]{standalone}
\usetikzlibrary{shapes.arrows}
\makeatletter
\def\createshadingfromlist#1#2#3{%
\pgfutil@tempcnta=0\relax
\pgfutil@for\pgf@tmp:={#3}\do{\advance\pgfutil@tempcnta by1}%
\ifnum\pgfutil@tempcnta=1\relax%
\edef\pgf@spec{color(0)=(#3);color(100)=(#3)}%
\else%
\pgfmathparse{50/(\pgfutil@tempcnta-1)}\let\pgf@step=\pgfmathresult%
%
\pgfutil@tempcntb=1\relax%
\pgfutil@for\pgf@tmp:={#3}\do{%
\ifnum\pgfutil@tempcntb=1\relax%
\edef\pgf@spec{color(0)=(\pgf@tmp);color(25)=(\pgf@tmp)}%
\else%
\ifnum\pgfutil@tempcntb<\pgfutil@tempcnta\relax%
\pgfmathparse{25+\pgf@step/4+(\pgfutil@tempcntb-1)*\pgf@step}%
\edef\pgf@spec{\pgf@spec;color(\pgfmathresult)=(\pgf@tmp)}%
\else%
\edef\pgf@spec{\pgf@spec;color(75)=(\pgf@tmp);color(100)=(\pgf@tmp)}%
\fi%
\fi%
\advance\pgfutil@tempcntb by1\relax%
}%
\fi%
\csname pgfdeclare#2shading\endcsname{#1}{100}\pgf@spec%
}
\createshadingfromlist{shading1}{vertical}{red,yellow,green,cyan,blue}
\createshadingfromlist{shading2}{vertical}{red,yellow}
\createshadingfromlist{shading3}{vertical}{black,blue,cyan,white}
\begin{document}
\begin{tikzpicture}[colorbar arrow/.style={
shape=double arrow,
double arrow head extend=0.125cm,
shape border rotate=90,
minimum height=5cm,
shading=#1
}]
\node [colorbar arrow=shading1] at (0,0) {};
\node [colorbar arrow=shading2] at (1,0) {};
\node [colorbar arrow=shading3] at (2,0) {};
\end{tikzpicture}
\end{document}
![enter image description here](https://i.stack.imgur.com/JXjjZ.png)
Best Answer
Hmm, so I'm the only one who thought the requirement was to shade an arrowhead on a path. Oh well...
Decorations can be used, but are slow. The following is far from perfect, but is basically usable. In the first two versions transformations like scaling and rotation will ruin everything, this is (mostly) corrected in the third version.
The approach involves creating a fading from the required path, which in principle is easy, but to reduce the amount of typing involved I've tried to automate it using a key. Some fooling around has to be done to stop the arrowheads getting cut off.
OK, so here is a version in which the path can be specified in the usual way. A bit more fooling around has to be done to get the bounding boxes right. The result is the same as above.
So, version 3. This one enables everything to be specified in the 'usual' way and the
shade path
key sorts everything else out. Unlike the previous versions this version should take care of transformations like rotation, shifting, scaling and so on. Like the previous versions however, it is basically a colossal hack: all sorts of tortuous coding is used to make things work, and may break in some circumstances. The result is the same as the first picture.Note also, that it is possible to say
draw=white
rather thandraw=transparent!0
.