# Color gradient fill between three corners of a triangle

Updates

Update1: Tried to implement solution by Harish Kumar (below)

Update2: Added a real example of what I would like to get in the end. I have a feeling I tried to leave to many things out.

Update3: added pstrick tag due to proposed answer below.

Update 2: I would like to get an image like the one below just with the colours changing gradually between cities. and this is what I got so far now trying to implement @AndrewStacey sugestion from here:

Original

To illustrate some data on a map I have given each location that falls in one out of twelve conditions a color. What i would like to do that the color slowly transitions between two locations. In the example below the three points 8, 18 and 28 represent three cities with different conditions.

Other than in this picture I would like the color to change gradually from blue (at 8) to green (at 28) and from green to ... you get the idea.

I found lots of examples to do this for two colours but couldn't find anything about mixing three colours.

The fill does not need to be a circle. Best would be if only the inside of the triangle is filled.

Is there a way to get this working?

The code I have used for the example above:

\documentclass[svgnames, tikz]{standalone}

% Needed for Definitions
\usepackage{siunitx} % for e.g. degree celsius
\usepackage{pgfplots}
\usepackage{xcolor}

\tikzset{city/.style={
% The Shape:
circle,
inner sep=0mm,
fill=red,
minimum size=0.5mm
}}

\tikzset{fortyone/.style={
% The Shape:
circle,
inner sep=0mm,
fill=green,
opacity=0.5,
minimum size=15mm
}}

\tikzset{fortytwo/.style={
% The Shape:
circle,
inner sep=0mm,
inner color=DarkRed,
outer color=red,
opacity=0.5,
minimum size=15mm
}}

\tikzset{fortythree/.style={
% The Shape:
circle,
inner sep=0mm,
fill=blue,
opacity=0.5,
minimum size=15mm
}}

\begin{document}
\begin{tikzpicture}
% Locations on map
\coordinate (A) at (0,0);
\coordinate (B) at (1,0);
\coordinate (C) at (0,1);

%%% Temporary conections
\draw (A) -- (B) -- (C) -- (A);

%%% Areas
\node at (A) [fortyone] {};
\node at (B) [fortytwo] {};
\node at (C) [fortythree] {};

%%% Cities and Labels
\node at (A) [city] {}; \node[left, font=\tiny] at (A) {28};
\node at (B) [city] {}; \node[right, font=\tiny] at (B) {18};
\node at (C) [city] {};  \node[left, font=\tiny] at (C) {8};

\end{tikzpicture}
\end{document}


Update 1: I have tried to implement @Harish Kumar solution. It works good for one triangle but as there are more I run into a number of problems.

and the code:

\documentclass[svgnames, tikz]{standalone}

% Needed for Definitions
\usepackage{siunitx} % for e.g. degree celsius
\usepackage{pgfplots}
\usepackage{xcolor}
\usetikzlibrary{shadings}

\tikzset{city/.style={
% The Shape:
circle,
inner sep=0mm,
fill=red,
minimum size=0.5mm
}}

\begin{document}
\begin{tikzpicture}
% Locations on map
\coordinate (A) at (0,0);
\coordinate (B) at (1,0);
\coordinate (C) at (0,1);
\coordinate (D) at (-1,0);
\coordinate (E) at (-1,-1);
\coordinate (F) at (1,1);

\begin{scope}
\clip (A) -- (B) -- (C) -- cycle;
\shade[upper left=blue,upper right=white,
lower left=green,lower right=DarkRed]
([xshift=-0.cm]C) rectangle (B);
\end{scope}

\begin{scope}
\clip (A) -- (D) -- (C) -- cycle;
\shade[upper right=blue,upper left=white,
lower right=green,lower left=blue]
([xshift=-0.cm]C) rectangle (D);
\end{scope}

\begin{scope}
\clip (B) -- (F) -- (C) -- cycle;
\shade[upper right=DarkRed,upper left=blue,
lower right=DarkRed,lower left=green]
([xshift=-0.cm]C) rectangle (B);
\end{scope}

\begin{scope}
\clip (D) -- (E) -- (A) -- cycle;
\shade[upper right=green,upper left=blue,
lower right=DarkRed,lower left=green]
([xshift=-0.cm]E) rectangle (A);
\end{scope}

\begin{scope}
\clip (A) -- (E) -- (B) -- cycle;
\shade[upper right=DarkRed,upper left=green,
lower right=DarkRed,lower left=green]
([xshift=-0.cm]E) rectangle (B);
\end{scope}

%%% Cities and Labels
\node at (A) [city] {}; \node[below, font=\tiny] at (A) {A};
\node at (B) [city] {}; \node[right, font=\tiny] at (B) {B};
\node at (C) [city] {};  \node[above, font=\tiny] at (C) {C};
\node at (D) [city] {};  \node[left, font=\tiny] at (D) {D};
\node at (E) [city] {};  \node[left, font=\tiny] at (E) {E};
\node at (F) [city] {};  \node[above, font=\tiny] at (F) {F};

\end{tikzpicture}
\end{document}


• The black lines are just there for the construction but not needed in the final image! May 20, 2014 at 15:45
• Did you see tex.stackexchange.com/q/14613/86? May 20, 2014 at 17:38
• Filling a polygon using triangles is not an easy task. I looked into this recently for another system and even for quadrilaterals it's not trivial (see loopspace.mathforge.org/discussion/38/filling-a-quadrilateral for what I came up with). So I think you need to think a bit more carefully about what you want the result to be. May 20, 2014 at 18:29
• @AndrewStacey Yes, I did. But I must have scrolled past the accepted solution. I will try that one now. May 20, 2014 at 18:34
• @AndrewStacey Your solution from the other thread might just work. Do you know if it is possible to use something other than south and co to determine the path? It would be great if I could say towards one of the corners or the connection between two points. May 20, 2014 at 19:06

## 2 Answers

Run with xelatex. It uses the Gouraud shading (http://en.wikipedia.org/wiki/Gouraud_shading):

\documentclass[pstricks]{standalone}
\usepackage{pstricks-add}
\begin{document}
\psset{unit=3}
\begin{pspicture}(-1,-1)(2,2)
\pnodes(0,0){A}(1,0){B}(0,1){C}
\pscircle*[linecolor=green,opacity=0.4](A){0.75}
\pscircle*[linecolor=red,opacity=0.4](B){0.75}
\pscircle*[linecolor=blue,opacity=0.4](C){0.75}
\psGTriangle(A)(B)(C){green}{red}{blue}
\psdots[linecolor=red](A)(B)(C)
\end{pspicture}
\end{document}


and the second example:

\documentclass[pstricks]{standalone}
\usepackage{pstricks-add}
\begin{document}

\psset{unit=3}
\begin{pspicture}(-0.2,-0.2)(2.25,2.25)
\pnodes(0,0){E}(1,1){A}(0,1){D}(1,2){C}(2,2){F}(2,1){B}
\psGTriangle(E)(D)(A){green}{blue}{green}
\psGTriangle(E)(A)(B){green}{green}{red}
\psGTriangle(D)(A)(C){blue}{green}{blue}
\psGTriangle(C)(A)(B){blue}{green}{red}
\psGTriangle(C)(F)(B){blue}{red}{red}
\psdots[linecolor=red](A)(B)(C)(D)(E)
\uput[-90](A){A}\uput[0](B){B}\uput[90](C){C}
\uput[180](D){D}\uput[180](E){E}
\end{pspicture}

\end{document}


• Ok, this looks really great. Is it possible to use it as indicated in the second picture of my original question? I have never used pstricks so far. What I need is to be able to put a map (country borders only) on top of the colour fill and have both scale together. Currently I am using this: \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.9\textwidth]{europe-ben02.eps}}; \begin{scope}[x={(image.south east)},y={(image.north west)}] ... May 21, 2014 at 7:50
• It's the same for PSTricks: \rput[lb](0,0){\includegraphics{...}}. lb for left-bottom
– user2478
May 21, 2014 at 11:14
• I have sadly run out of time to implement this in my current document and will need to stick to my original images but I believe this would be the way to go. If I ever revisit this topic I will definitely try to use this approach. May 26, 2014 at 8:40

Here is some crude method using bilinear interpolation shading and clip:

\documentclass[svgnames, tikz]{standalone}

% Needed for Definitions
\usepackage{siunitx} % for e.g. degree celsius
\usepackage{pgfplots}
\usepackage{xcolor}
\usetikzlibrary{shadings}

\tikzset{city/.style={
% The Shape:
circle,
inner sep=0mm,
fill=red,
minimum size=0.5mm
}}

\tikzset{fortyone/.style={
% The Shape:
circle,
inner sep=0mm,
fill=green,
opacity=0.5,
minimum size=15mm
}}

\tikzset{fortytwo/.style={
% The Shape:
circle,
inner sep=0mm,
inner color=DarkRed,
outer color=red,
opacity=0.5,
minimum size=15mm
}}

\tikzset{fortythree/.style={
% The Shape:
circle,
inner sep=0mm,
fill=blue,
opacity=0.5,
minimum size=15mm
}}

\begin{document}
\begin{tikzpicture}
% Locations on map
\coordinate (A) at (0,0);
\coordinate (B) at (1,0);
\coordinate (C) at (0,1);

%%% Temporary conections
%\draw (A) -- (B) -- (C) -- (A);

%%% Areas
\node at (A) [fortyone] {};
\node at (B) [fortytwo] {};
\node at (C) [fortythree] {};

%%% Cities and Labels
\node at (A) [city] {}; \node[left, font=\tiny] at (A) {28};
\node at (B) [city] {}; \node[right, font=\tiny] at (B) {18};
\node at (C) [city] {};  \node[left, font=\tiny] at (C) {8};

\begin{scope}
\clip (A) -- (B) -- (C) -- cycle;
\shade[upper left=blue,upper right=white,
lower left=green,lower right=DarkRed]
([xshift=-0.2cm]C) rectangle (B);
\end{scope}

\end{tikzpicture}
\end{document}