Flash
  Home arrow Flash arrow Page 2 - Flash Hack A Custom Color Transform Class
Dev Articles Forums 
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  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FLASH

Flash Hack A Custom Color Transform Class
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2004-10-12

    Table of Contents:
  • Flash Hack A Custom Color Transform Class
  • Code Listing: A Custom Transform Class
  • A Closer Look at the Code
  • Constructor Function Transform()
  • Enhancing the Custom Class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

    More Flash Articles
    More By O'Reilly Media


     

    FLASH ARTICLES

    - 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
    - Organizing Frames and Layers for Flash Anima...
    - Organizing Frames and Layers
    - Using XML and ActionScript with Flex Applica...
    - Interfaces and Events with ActionScript and ...
    - Manipulating Data with ActionScript in Flex ...
    - ActionScript Syntax for Flex Applications







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek