
how to create a three points gradient with the drawing API methods.
since my (early) Delaunay triangulation attempts (2006!) I’ve wanted to draw a 3 point gradient.
like for instance you give some vertices, associate them with colors and it would draw a smooth, super complex gradient between the vertices… still dreaming of it though.
lately pixelero blew my attempts to smithereens when he released a Pixel Bender shader to do just that: Three Point Gradient.
actually this article by omino is also interesting.
Now if you work with the FP10, you may stop reading here ; using the ShaderFill is 10 * faster than the method described hereafter. it is also far more accurate.
still here? :)
ok, the biggest interest of the method is that the algorithm can work with AS1 / FP6+ files.
yes, you heard right, in the FP6 release, the API already had the beginGradientFill method and the Point & Matrix Objects which is approximately all we need to do the thing.
here’s a quick demo click, click, clik to draw triangles:
and here’s how it works (love thoses schemes ^^):

so the colors are drawn one after the other which means that the result irectly depends on the colors order.
that’s why I’ve added a baseColorMethod. this will draw the triangle once with the given color to help reinforce the colors. in the demo below it’s a grayscale to enhance the effect but it can be any color.
this does not happen with the pixel bender version for each pixel is a mathematical interpolation. with PB the colors are brighter and exact…
I had a hard trying to solve the matrix issues and finally used Quasimondo’s matrix class (thanks :) )
the good thing is that the very same result can be achieved in the FP6 !
ok, apart from banners there are no real reasons to use such an old version of the player but it might come in handy if you have to .. say.. hem.. do an animated three pointrs gradients in a FP6 banner ^^’
wellwellwell
so here’s a comparison between Pixelero’s Pixel Bender shader (on the left) and my method (on the right ).
you’ll find the class here : GradientTriangle.as
you can use it as follow:
var gt:GradientTriangle = new GradientTriangle( p0, p1, p2, c0, c1, c2 ); gt.render( graphics );
with p0, p1, p2, being Points and c0, c1, c2 their associated colors.
ok I haven’t ported it to AS1 but fell free to do so :)
and finally for your eyes only I’ve coupled the PB filter with a Delaunay triangulator to create nice pictures like this.
you can tune the opacities of the bitmaps. you can click to create triangles manually or try to catch canny edges. keep the canny scale low (around 10 ) or it might just crash.
it should look like the pictures hereunder with rather high scales.


(a cover of the crystal castles album)

you can also set a random cloud of points.
this is a direct allusion to jonathan puckey
enjoy :)
FlashDevelop
HAXE
leerraum
on Oct 4th, 2009
@ 9:32 am:
ok, thats wow!
felix
on Oct 5th, 2009
@ 7:06 am:
sweet!
Andreas Weber
on Oct 5th, 2009
@ 9:30 am:
Very cool, especially in combination with the Delaunay triangulator!
Years ago I wrote a class for 4-point gradients:
http://www.motiondraw.com/blog/?p=52
It’s quite slow as it fills the rectangle by adding 1-pixel wide lines.
nicoptere
on Oct 5th, 2009
@ 11:15 am:
@all : thanks, as usual, it’s gratifying to have such positive feedbacks :)
@andreas: I haven’t mentioned your article which is a shame for that’s where I started.
I’d like to mention another sweet (yet too CPU intensive) attempt by my esteemed friend grgrdvrt: http://grgrdvrt.com/blog/15/200607038-interpolations-de-couleurs/
makc3d
on Oct 5th, 2009
@ 12:54 pm:
nice. afair in fp8+ you can take 2×2 bitmap, run three setpixels, and map that with beginBitmapFill, no need to go for fp10 shaders.
piXelero
on Oct 9th, 2009
@ 9:32 pm:
Nice images !
… and in fp10 you can also take a 2×2 bitmap, run three setpixels, and map that with drawTriangles, no matrix operations …
nicoptere
on Oct 13th, 2009
@ 1:42 pm:
thanks to makc and pixelero it starts looking like an exhaustive set of solutions
I love this community :)
Amit Patel
on Oct 13th, 2009
@ 5:34 pm:
Cool! I was going to suggest BlendMode.ADD to make it order-independent (and brighter) but that’s Flash 9. Also for Flash 9, I used BitmapData.paletteMap() for gradients: http://simblob.blogspot.com/2009/03/flash-9-triangle-gradients.html
benjamin
on Oct 19th, 2009
@ 5:03 pm:
“ok, the biggest interest of the method is that the algorithm can work with AS1 / FP6+ files.”
please, avoid telling future costumers you can do wonderfull things with as1 (sure we can, but if they could avoid to ask me…)
Great work, very inspiring !!!
;-)
makc3d
on Mar 12th, 2010
@ 2:56 pm:
btw, you have broken link to http://www.petrileskinen.fi/Actionscript/ThreePointGradient/ThreePointGradientPBK.html
nicoptere
on Mar 12th, 2010
@ 4:43 pm:
thanks, fixed :)