I have a triangle with a value attached to each node (e.g. (A,B,C) -> (0.3, 0.1, 0.7)
). The values are mapped to an RGB color with the following function (0.0: blue, 0.5: green, 1.0: red):
var color = Color.FromArgb(alpha,
(int)((value > 0.5 ? 2*value - 1 : 0) * 255),
(int) ((value > 0.5 ? 2-2*value : 2*value) * 255),
(int)((value > 0.5 ? 0 : (1 - 2 * value)) * 255)
);
These values are meant to be interpolated linearly on the triangle (e.g. weight by barycentric coordinates) and it's a larger mesh of triangles in the full application.
For drawing, I create a GraphicsPath
with points on each of the three corners and several points in between (12 in the example which is probably overkill). On a PathGradientBrush
, I set the SurroundColors
colors to either the values of the corners or a linear interpolation between those. The CenterPoint
and the CenterValue
are set to (A+B+C)/3
and values.Sum()/3
. This looks good for some values (here (0.0, 0.5, 1.0)
:
but not so good for others (here (0.3, 0.8, 1.0)
, see the artifact at the top of the triangle):
I tried several other strategies (e.g. weighting the values by distance to the center point, placing the center somerwhere else, etc.) but still getting those artifacts. I only need triangles but the drawing must happen in a Paint-handler and painting manually is too slow. How can I
- either get rid of the center color and get a colorized triangle like in OpenGL
- or get a center value and/ or position which doesn't lead to those artifacts.
Code to reproduce the issue and play around (a bit too much to paste it into the question): http://pastebin.com/gjjzqRvY