DS HomeDev Shed | ASP Free | Dev Articles | Scripts | Dev Hardware | Dev Archives | SEO Chat | Dev Mechanic | Web Hosting
      Flash
igrep Developer Search
Home arrow Flash arrow Flash Hack A Custom Color Transform Class
 
igrep Developer Search
  ADO.NET  
  Apache  
  ASP  
  ASP.NET  
  C#  
  C++  
  ColdFusion  
  COM/COM+  
  Delphi/Kylix  
  Design Usability  
  Development Cycles  
  DHTML  
  Embedded Tools  
  Flash  
  Graphic Design  
  HTML  
  IIS  
  Interviews  
  Java  
  JavaScript  
  MySQL  
  Oracle  
  Photoshop  
  PHP  
  Reviews  
  SQL  
  SQL Server  
  Style Sheets  
  VB.Net  
  Visual Basic  
  Web Authoring  
  Web Services  
  Web Standards  
  XML  
  Developer Forums  
  Forums Archive  
  Get Daily Updates  
  Plug In PDF Mag.  
  Developer Updates  
  Free Website Content  
  Weekly Newsletter  
  Web Hosting
 
  ASP Web Hosting
 
  ASP.NET Web Hosting  
  Budget Hosting  
  Coldfusion  
  Colocation  
  Dedicated Servers
 
  E-Commerce Hosting  
  Linux Web Hosting
 
  Managed Hosting  
  Reseller Web Hosting
 
  Shared Hosting  
  Small Business Hosting
 
  Virtual Private Servers
 
  Windows Web Hosting
 
  Articles:  
Blogs:  
  Forums:  
  Blog History  
  Article Discussion  
  Games  
  Hardware  
  How Tos  
  Law  
  Microsoft  
  Open Source  
  Security  
  SEO  
  Software  
  Technology News  
  Web Hosting  
  Wow  
  Past Technology News
  Apress Books
  Write For Us Get Paid  
  Request Media Kit
  Contact Us  
  Site Map  

Flash


Flash Hack A Custom Color Transform Class
Contributed by O'Reilly Media
Article Rating:starstarstarstarstar / 6
2004-10-12
Poor Best
[ Send Me Similar Content When Posted ]
 
[ Add Developer Shed Headlines To Your Site ]

Discuss This Article
DISCUSS
Developer Newsletter
NEWS
E-mail This Article
SEND
Print This Article
PRINT
PDF Version
PDF
 

Article Index:

  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
  6. Search For More Articles!
  7. Author Terms
 
 
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
 }
}

Generated in 0.559330 sec.