## Weblog

### flower power

I think no one, besides MS (if even), totally understand how their gradient works
(**please feel free to prove me wrong!** :-). The `LinearGradientBrush`

API
accept a rectangle (or two points), two colors, an angle (which can be scalable or not) and a matrix
which gets initialized to some *values*. There are also a few more parameters that
can be converted to the previous ones. Compare that to supplying
Cairo
where you have to supply two points, two colors and, optionally, apply a matrix.

I'm not a very graphic-oriented guy, so the initial matrix didn't any make sense to me (from a math point of view) until I plotted it's results.

*it was the first flower I saw this spring (and yes the yellow offset is normal)*

and here's the flower's source code...

private void Form1_Paint (object sender, PaintEventArgs e)
{
Rectangle rect = new Rectangle (0, 0, 100, 100);
Color c1 = Color.Red;
Color c2 = Color.Black;
Pen pen = new Pen (Color.Green);
for (int a = 0; a < 360; a++) {
LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, a, false);
float[] elements = lgb.Transform.Elements;
e.Graphics.DrawLine (pen, 150, 150, 150 + elements[0] * 100, 150 + elements[1] * 100);
}
pen.Dispose ();
pen = new Pen (Color.Yellow);
for (int a = 0; a < 360; a++) {
LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, a, false);
float[] elements = lgb.Transform.Elements;
e.Graphics.DrawLine (pen, 150, 150, 150 + elements[4], 150 + elements[5]);
}
pen.Dispose ();
}

Anyway, libgdiplus' gradients are still not perfect (but the matrix math should be now :-), mostly when gradients are rotated, but the results on ZedGraph are encouraging.

*The gradient angle isn't quite right compared to the
original...*

4/21/2006 10:01:17 | Comments | Permalink

**The views expressed on this website/weblog are mine alone and do not necessarily reflect the views of my employer.**