Setting color values

You can use the methods of the built-in ColorTransform class (flash.geom.ColorTransform) to adjust the color of a movie clip. The rgb property of the ColorTransform class assigns hexadecimal red, green, blue (RGB) values to the movie clip. The following example uses rgb to change an object's color, based on which button the user clicks.

To set the color value of a movie clip:

  1. Create a new Flash document and save it as setrgb.fla.
  2. Select the Rectangle Tool and draw a large square on the Stage.
  3. Convert the shape to a movie clip symbol and give the symbol an instance name of car_mc in the Property inspector.
  4. Create a button symbol named colorChip, place four instances of the button on the Stage, and name them red_btn, green_btn, blue_btn, and black_btn.
  5. Select Frame 1 in the main Timeline, and select Window > Actions.
  6. Add the following code to Frame 1 of the main Timeline:
    import flash.geom.ColorTransform;
    import flash.geom.Transform;
    
    var colorTrans:ColorTransform = new ColorTransform();
    var trans:Transform = new Transform(car_mc);
    trans.colorTransform = colorTrans;
    
  7. To make the blue button change the color of the car_mc movie clip to blue, add the following code to the Actions panel:
    blue_btn.onRelease = function() {
        colorTrans.rgb = 0x333399; // blue
        trans.colorTransform = colorTrans;
    };
    

    The preceding snippet of code changes the rgb property of the color transform object and reapplies the color tranform effect to the car_mc movie clip whenever the button is pressed.

  8. Repeat step 7 for the other buttons (red_btn, green_btn, and black_btn) to change the color of the movie clip to the corresponding color.

    Your code should now look like the following example (new code is in bold):

    import flash.geom.ColorTransform;
    import flash.geom.Transform;
    
    var colorTrans:ColorTransform = new ColorTransform();
    var trans:Transform = new Transform(car_mc);
    trans.colorTransform = colorTrans;
    
    blue_btn.onRelease = function() {
        colorTrans.rgb = 0x333399; // blue
        trans.colorTransform = colorTrans;
    };
    red_btn.onRelease = function() {
        colorTrans.rgb = 0xFF0000; // red
        trans.colorTransform = colorTrans;
    };
    green_btn.onRelease = function() {
        colorTrans.rgb = 0x006600; // green
        trans.colorTransform = colorTrans;
    };
    black_btn.onRelease = function() {
        colorTrans.rgb = 0x000000; // black
        trans.colorTransform = colorTrans;
    };
    
  9. Select Control > Test Movie to change the color of the movie clip.

For more information about the methods of the ColorTransform class, see ColorTransform (flash.geom.ColorTransform) in the ActionScript 2.0 Language Reference.