How do I draw a triangle in tikz with "point colours" so that every point in the triangle is coloured according to the weighted (by distance) average colour of the corners.
I feel that the other answers given are perhaps a little overcomplicated! If you want the triangle exact then maybe they are the best way to go. But if you want something that just looks about right, then there is a much simpler way to do this using ordinary fadings.
(Added in edit: I've updated this a little to try to correct the colour bias. The red colour is now correct and the green/blue are relatively correct. That is, the green and blue are correct at the bottom of the triangle, but as you move up the sides then some blue gets mixed in with the green and vice-versa. However, before it gets too noticeable, the red swamps the picture so it's actually quite close to the Real Thing.)
Here's the result:
Here's the code:
PGF provides functional shadings. The following works by calculating the barycentric coordinates of a equilateral triangle from the Cartesian coordinates (see Wikipedia) and using those as the RGB color.
The PGF manual has a warning:
In fact, Evince (and probably most Linux pdf viewers) renders the above document as
I keep playing around with this. The most portable solution seems to be the one by Altermundus. Here it is encapsulated into a macro and with some optimizations
For sufficiently small many subdivision, the triangle looks passably smooth and should render correctly in most PDF viewers. It does however draw O(subdivisions²) rectangles and compilation time scales accordingly. So you might want to use the externalization library of TikZ. The above example compiles in about 2.9s on my computer and produces
I am fully aware that this post is quite old.
However, due to the remark of Lev Bishop that this would be very simple if TikZ would support "PDF type 4 shadings (free-form Gouraud-shaded triangle mesh)", I wanted to add a matching example.
Here is a type 4 shading with explicitly colored vertices:
The example above is a
This example can be generalized easily to more than one triangle (or other primitive forms like rectangles or bezier shapes). Here is an example with different color specifiers which results in three triangular patches:
Finally, this kind of shading is sometimes quite useful if you have scalar color values which are mapped into a
This last example makes it very clear that triangle interpolation is linear.
Only for the fun but perhaps it's possible with postcript macros 84.2.3 General (Functional) Shadings of pgfmanual cvs. The code is very slow to compile a better will be to draw triangles (node) instead of circles. The Caramdir's answer proves that my idea was fine. I use the comment to change my answer, always for the fun ...This is perhaps always wrong but it's a more correct answer than my first one.
Just one more way to get a Gouraud-shaded triangle:
To get a standalone
Edit (suggested by Andrew Stacey):