Home arrow Flash arrow Page 2 - Flash Hack A Custom Color Transform Class
FLASH

Flash Hack A Custom Color Transform Class


See how to implement a custom color transform class in ActionScript 2.0 using object-oriented programming (OOP) instead of using a procedural timeline-based code. (From the book, Flash Hacks, by Sham Bhangal, O'Reilly Media, 2004, ISBN: 0596006454.)

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 14
October 12, 2004
TABLE OF CONTENTS:
  1. · Flash Hack A Custom Color Transform Class
  2. · Code Listing: A Custom Transform Class
  3. · A Closer Look at the Code
  4. · Constructor Function Transform()
  5. · Enhancing the Custom Class

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Flash Hack A Custom Color Transform Class - Code Listing: A Custom Transform Class
(Page 2 of 5 )

Although we can’t give a full course on OOP and ActionScript 2.0 here, this custom color transform class can be used even if you don’t understand OOP. And we’ll examine several aspects of the code after the code listing.

Here is our object-oriented version, implemented as a custom Transform class, which must be stored in an external Transform.as file:

// This ActionScript 2.0 code must go in an external Transform.as file
class Transform {
  // NEG_TRANS inverts the color values.
  // NEUTRAL_TRANS resets the color values.
  // BLACK_TRANS sets the color values to black.
  // WHITE_TRANS sets the color values to white.
  // RATE sets the rate the effects will run at in ms.
private static var NEG_TRANS:Object = {ra:-100, rb:255,
           ga:-100, gb:255, ba:-100, bb:255, aa:100, ab:0};
private static var NEUTRAL_TRANS:Object = {ra:100, rb:0,
           ga:100, gb:0, ba:100, bb:0, aa:100, ab:0};

private static var BLACK_TRANS:Object = {ra:100, rb:-255,
          ga:100, gb:-255, ba:100, bb:-255, aa:100, ab:0};
private static var WHITE_TRANS:Object = {ra:100, rb:255,
          ga:100, gb:255, ba:100, bb:255, aa:100, ab:0};
private static var RATE:Number = 50;

private var interval:Number;
private var startTime:Number;
private var colorObj:Color;
// Constructor accepts target clip to which to apply transforms
public function Transform(targetClip:MovieClip) {
 colorObj = new Color(targetClip);
}

// Inverts the color values
public function invert(duration:Number):Void {
 applyTransform(NEG_TRANS, duration);
}

// Resets the color to the default values set in the authoring tool
public function reset(duration:Number):Void {
 applyTransform(NEUTRAL_TRANS, duration);
}

// Performs a fade to black over specified duration in ms
public function fadeToBlack(duration:Number):Void {
 applyTransform(BLACK_TRANS, duration);
}

// Performs a fade to white over specified duration in ms
public function fadeToWhite(duration:Number):Void {
 applyTransform(WHITE_TRANS, duration);
}

// Function to initiate a fade and set up an interval to
// complete it over time.
private function applyTransform(transObject:Object,
                               duration:Number):Void {
var getTrans:Object = colorObj.getTransform( );
var diffTrans:Object = new Object( );
startTime = getTimer( );
for (var i in transObject) {
diffTrans[i] = (transObject[i] - getTrans[i]) / (duration / RATE);
}
// Use the form of setInterval( ) that invokes a method of an object,
// so that instance properties are in scope (the object is this).
// First parameter is the object (this) on which to invoke the
// method specified by the second parameter (in this case
// "transition", which must be passed as a string).
// Third parameter is interval duration in ms.
// Fourth, fifth, and sixth parameters get passed to transition( )
interval = setInterval(this, "transition", RATE, transObject, diffTrans,
duration);
}

// This method applies each step of the color transformation.
private function transition(transObject:Object, diffTrans:Object,
duration:Number):Void {
  var getTrans:Object = colorObj.getTransform( );
  for (var i in diffTrans) {
    getTrans[i] += diffTrans[i];
  }
  colorObj.setTransform(getTrans);
  if (getTimer( ) - startTime > duration) {
    // Complete the final step in the transition
    colorObj.setTransform(transObject);
    // Clear the interval to stop the effect
    clearInterval(interval);
  }
  // Force the screen to refresh between frames
  updateAfterEvent( );
 }
 public function die( ):Void {
   // Perform any cleanup code here
 }
}

 

Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

Visit the O'Reilly Network http://www.oreillynet.com for more online content.


blog comments powered by Disqus
FLASH ARTICLES

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials