7

I would like to draw a shaded triangle by defining the 3 vertices and an RGB color at each of those vertices. The interior shaded color would vary linearly from one vertex to the other.

Here is what I have so far but the shading is not what I am needing.

\documentclass[tikz]{standalone}

\usetikzlibrary{calc,intersections}

\begin{document}
\begin{tikzpicture}

% Specify the coordinates
\coordinate (A1) at (0,0);
\coordinate (A2) at (5,2);
\coordinate (A3) at (10,-3);

\definecolor{c1}{RGB}{0,129,188}
\definecolor{c2}{RGB}{252,177,49}
\definecolor{c3}{RGB}{35,34,35}

% Draw the sides of the triangle
\draw (A1) -- (A2) -- (A3) -- cycle;

\shade [left color=c1,right color=c2] (A1) -- (A2) -- (A3) -- cycle;

\end{tikzpicture}
\end{document}

While this question is similar: Tikz triangle with point colours

The answers are limited in the following ways

  • Several of the solutions are artistic approximations and not precise shading between the vertices.
  • The solutions are presented for an equilateral triangle and not a general triangle.
  • The solution method using pgfplots works the best but won't display in ShareLatex and is having difficulty printing so is limited.
3
  • 5
    Have you seen Tikz triangle with point colours Mar 5, 2015 at 17:53
  • Hej Torbjorn. I've been searching but had not seen that post. The pgfplots solution may work the best for what I need. Mvh, A. Hustrulid
    – Andrew
    Mar 5, 2015 at 18:05
  • By "varying linearly", do you mean linearly in RGB values?
    – Symbol 1
    Mar 7, 2015 at 3:44

1 Answer 1

3

Just to answer the unanswered.

enter image description here

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}

\definecolor{c1}{RGB}{0,129,188}
\definecolor{c2}{RGB}{252,177,49}
\definecolor{c3}{RGB}{35,34,35}

\begin{tikzpicture}
\begin{axis}[hide axis]
    \addplot[
        patch,
        shader=interp,
        mesh/color input=explicit,
        data cs=cart,
    ]
    coordinates {
        (0,0)   [color=c1]
        (5,2)   [color=c2]
        (10,-3) [color=c3]
    };
\end{axis}
\end{tikzpicture}
\end{document}

One of the easiest ways to do this is using pgfplots. Many other solutions can be found out here on tex.sx with different degrees of complexity and accuracy. But this seems simple with acceptable output.

Here I draw within an axis but hide it. This is essentially a plot with interpolated shading and the vertices colors are given explicitly.

3
  • This seems to be basically code from an answer in the linked question minus the axes. Is some credit due or is this an independent solution? (Certainly possible but you mention surveying existing solutions in your question...)
    – cfr
    Jul 17, 2015 at 1:45
  • It also doesn't answer the OP's question, does it? Since that explicitly asks for a solution which doesn't have problems arising from the use of pgfplots.
    – cfr
    Jul 17, 2015 at 1:46
  • @cfr - My solution is 1) not artistic 2) for general triangle 3) prints OK, I did it. Regarding ShareLatex , it compiles OK, but the previewer doesn't show the result correctly (which is the case for any other solution). This is off-topic and the OP can contact the maintainers. My self, I open the result directly in the browser by pressing "Download PDF". For citing, I said "Many other solutions can be found out here on tex.sx" and didn't repeat the same source which is already mentioned by the OP. I and the OP agree that "pgfplots works the best".
    – AboAmmar
    Jul 17, 2015 at 2:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .