Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>// Here's how I did it. I wrote &amp; tested this in Flash CS4. // I rambled about this technique in my blog: // http://blog.zacharcher.com/2007/10/13/understanding-colormatrixfilter/ // The goal is to create a simple black-and-white gradient, then tween its ColorTransform, // rather than redraw a new gradient every frame. // I'm going to use Tweener as my tweening engine because I'm used to it. // Download from here: http://code.google.com/p/tweener/downloads/list import caurina.transitions.Tweener; import caurina.transitions.properties.ColorShortcuts; ColorShortcuts.init(); // First, create a Shape with a gradient fill. var shp:Shape = new Shape(); var mtx:Matrix = new Matrix(); // This matrix is needed for Shape.beginGradientFill() mtx.createGradientBox( stage.stageWidth, stage.stageHeight, Math.PI/2 ); with( shp.graphics ) { beginGradientFill( GradientType.LINEAR, [0x000000,0xffffff], [1,1], [0,255], mtx ); drawRect( 0, 0, stage.stageWidth, stage.stageHeight ); endFill(); } // Draw the Shape inside some BitmapData. var bData:BitmapData = new BitmapData( stage.stageWidth, stage.stageHeight, false, 0x000000 ); bData.draw( shp ); // Create a Bitmap to display the BitmapData, and add it to the stage. var bmp:Bitmap = new Bitmap( bData ); addChild( bmp ); // For testing purposes: Set up a mouse click listener. When the user clicks, tween to new colors. stage.addEventListener( MouseEvent.CLICK, onClick ); function onClick( e:MouseEvent ) :void { // Choose two colors at random var c1:int = 0xffffff * Math.random(); var c2:int = 0xffffff * Math.random(); trace("Now tweening to:", c1.toString(16), c2.toString(16) ); colorChange( c1, c2 ); } // This function supports tweening the gradient to ANY TWO COLORS. // If you just want to tint a gradient, then you can use less scary code, // but you didn't specify that ;) function colorChange( c1:uint, c2:uint ) :void { // Split the incoming color uint's into RGB values, ranging from 0..255 var o1:Object = { r: (c1 &amp; 0xff0000) &gt;&gt; 16, g: (c1 &amp; 0x00ff00) &gt;&gt; 8, b: c1 &amp; 0x0000ff }; var o2:Object = { r: (c2 &amp; 0xff0000) &gt;&gt; 16, g: (c2 &amp; 0x00ff00) &gt;&gt; 8, b: c2 &amp; 0x0000ff }; // Using these values, create our sweet ColorTransform. // This will "map" the black and white pixels to the desired colors. // The aforementioned blog post explains this math: var ct:ColorTransform = new ColorTransform( (o2.r-o1.r)/255.0, (o2.g-o1.g)/255.0, (o2.b-o1.b)/255.0, 1, o1.r, o1.g, o1.b, 0 ); // Start the tween. Tweener.addTween( bmp, {_colorTransform:ct, time:1.0, transition:"easeOutQuad"} ); } // Hope this helps... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload