Get $500+ of the best After Effects files, video templates and music for only $20!
Create a Brightness Editor in ActionScript 3

Create a Brightness Editor in ActionScript 3

Tutorial Details
  • Difficulty: Beginner
  • Program: Flash
  • Estimated Completion Time: 30 mins

Brightness is an attribute of visual perception in which a source appears to be radiating or reflecting light.

In this tutorial we will learn how to modify the brightness of a Display Object using ActionScript 3.


Final Result Preview

Let’s take a look at the final result we will be working towards:


Step 1: Brief Overview

Using the ColorTransform class and a Slider component we will increase or decrease a DisplayObject’s brightness.


Step 2: Starting

Open Flash and create a new Flash File (ActionScript 3).

Set the stage size to 500x350px and set the frame rate to 24fps.


Step 3: Choose a Display Object

Any DisplayObject can be used with this class; for this example I picked an image from Flickr:

Place your image in the center of the stage, convert it to MovieClip and give it an instance name of image.


Step 4: Brightness Panel

We’ll create a panel with a Slider component and a Dynamic Textfield as interactive objects.

Select the Rectangle Primitive Tool (R) and draw a 250x70px rectangle, set the corner radius to 7 and change its alpha to 60.

Convert it to MovieClip and add the following filter:

The panel should look like this:

Double-click the MovieClip to enter edit mode and create a Dynamic Textfield; set its instance name to bValue (for “brightness value”) and center it. Give it an initial entry of 0. You can also add a title to the panel and some icons as a guide.


Step 5: Slider Component

Open the Components panel (Cmd/Ctrl + F7) and drag a Slider component to your Brightness panel; center it and name it slider.


Step 6: Component Inspector

With the slider component selected, press Shift+F7 to open the Component Inspector panel and edit the options as shown in the image:


Step 7: ActionScript

Create a new ActionScript Document and save it as Main.as.

We’re going to use a document class for this project. If you’re not sure how to use a document class, read this quick introduction.


Step 8: Package

package
{

The package keyword allows you to organize your code into groups that can be imported by other scripts, it is recommended to name them starting with a lowercase letter and use intercaps for subsequent words for example: myClasses.

If you don’t want to group your files in a package or you have only one class you can use it right from your source folder.


Step 9: Required Classes

A few classes are needed for this to work. For a more detailed description about every class, please refer to the Flash Help (F1).

import flash.display.Sprite;
import fl.events.SliderEvent;
import flash.geom.ColorTransform;

Step 10: Extending the Class

The extends keyword defines a class that is a subclass of another class. The subclass inherits all the methods, properties and functions, that way we can use them in our class.

public class Main extends Sprite
{

Step 11: Variables

Only one variable is needed in this example: a ColorTransform instance. This will be used to change the RGB values of the target display object.

var colorTransform:ColorTransform = new ColorTransform();

Step 12: Constructor Function

This function is executed when the class is loaded.

A SliderEvent listener is added to the Slider Component to run a function when the user changes the value of the Slider.

public function Main():void
{
	panel.slider.addEventListener(SliderEvent.CHANGE, updateBrightness);
}

Step 13: Color Transform

This function is executed when the slider value changes. This is the ColorTransform part. The red, green, and blue offsets of the color transform matrix are set to match the value of the slider.

private function updateBrightness(e:SliderEvent):void
{
   /* Set the RGB offsets to the slider value */

	colorTransform.redOffset = e.value;
	colorTransform.greenOffset = e.value;
	colorTransform.blueOffset = e.value;

Want to know what we’re doing here? Check out this article explaining color transformations.


Step 14: Apply Changes

This line applies the changes to the image on the stage.

image.transform.colorTransform = colorTransform;

Step 15: Text Value

The text in the brightness panel is also updated.

	panel.bValue.text = e.value;
}
}
}    

Step 16: Document Class

Go back to the .fla file and in the Properties Panel set the Class field to Main to link it to the Document Class.


Conclusion

Now you can easily implement a way to modify the brightness of an image or other Display Object using ActionScript 3.

Thank you for reading!

Add Comment

Discussion 11 Comments

  1. John says:

    This is a quick tip not a complete tutorial

  2. Greg says:

    Excellent tutorial!

    What about sliders for offsetting RGB colors, or contrast? I tried having just a slider for redOffset, but that didn’t seem to do it.

    • Javier says:

      The offset is for a diffferent thing. If you want to change RGB you need to create a new color changing red, green and blue one at a time.

      For example:

      red: 255
      green: 125
      blue: 0

      Apply this on a color transform and you get orange.

      Now lets say you want to get yellow, you would have to change the green value from 125 to 255.

      now we have:

      red: 255
      green: 255
      blue: 0

      Create a variable to hold the color we are going to create:

      color = red << 16 | green << 8 | blue

      Apply this on a color transform and you get yellow as expected. For those who may not now what i did, i used bit manipulation(BM). Colors are hex numbers, and the best way to deal with them is using BM.

    • eu4ria says:

      The way I did that was add 3 sliders, one each for red, green and blue. Change the min and max numbers in the bindings to -255 and 255. Then add this code to function called by your updateSlider event:

      colorTransform.redOffset = panel.rSlider.value;
      colorTransform.greenOffset = panel.gSlider.value;
      colorTransform.blueOffset = panel.bSlider.value;

      image.transform.colorTransform = colorTransform;

      panel.rValue.text = panel.rSlider.value;
      panel.gValue.text = panel.gSlider.value;
      panel.bValue.text = panel.bSlider.value;

      With all the values at 0 the image is the default. By adjusting the sliders down you can remove colour and by incresing them you can add colour to tint them.

  3. I’m allready thinking about integrating it with PHP and GD so the changes can be permanent. Maybe some sort of picture editing system? I know. It’s done before but still more nice to do these things for yourself :D
    +1

  4. Roy says:

    Simple, but useful!

  5. Lewis Cowles says:

    are there any more neat tricks like this because I think I’ll WonderFL this code too ;)

  6. TheDarkIn1978 says:

    There’s a much easier way to accomplish this by employing the AdjustColor class and a ColorMatrixFilter.

  7. Ravi says:

    Hi,

    Very informative but basic. I like the way you explained, Can you post a tut on complete editor as in photoshop hue/saturation palette.

    I have also created many examples in ActionSCript3.
    http://www.admecindia.co.in/flash-website.html

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.