Adjusting color values with ColorTransform class

August 5th, 2010 by Piotr Wierzgała Leave a reply »

The ColorTransform class allows you to adjust the color values of any display object. The color adjustment or color transformation can be applied to all four channels: red, green, blue, and alpha transparency.

Table of contents

Application

The application below lets you check how the ColorTransform class works. The application’s source is available under right mouse button context menu („view source”).

ColorTransform properties

  • redMultiplier: A decimal value that is multiplied with the red channel value.
  • redOffset: A number from -255 to 255 that is added to the red channel value after it has been multiplied by the redMultiplier value.
  • greenMultiplier: A decimal value that is multiplied with the green channel value.
  • greenOffset: A number from -255 to 255 that is added to the green channel value after it has been multiplied by the greenMultiplier value.
  • blueMultiplier: A decimal value that is multiplied with the blue channel value.
  • blueOffset: A number from -255 to 255 that is added to the blue channel value after it has been multiplied by the blueMultiplier value.
  • alphaMultiplier: A decimal value that is multiplied with the alpha transparency channel value. If you set the alpha transparency value of a display object directly by using the alpha property of the DisplayObject instance, it affects the value of the alphaMultiplier property of that display object’s transform.colorTransform property.
  • alphaOffset: A number from -255 to 255 that is added to the alpha transparency channel value after it has been multiplied by the alphaMultiplier value.
  • color: When you set this property, it changes the three color offset values accordingly, and it sets the three color multiplier values to 0. The alpha transparency multiplier and offset values do not change. When you pass a value for this property, use the format 0xRRGGBB. RR, GG, and BB each consist of two hexadecimal digits that specify the offset of each color component.

When a ColorTransform object is applied to a display object, a new value for each color channel is calculated in the following way:

  • new red value = (old red value * redMultiplier) + redOffset
  • new green value = (old green value * greenMultiplier) + greenOffset
  • new blue value = (old blue value * blueMultiplier) + blueOffset
  • new alpha value = (old alpha value * alphaMultiplier) + alphaOffset

If any of the color channel values is greater than 255 after the calculation, it is set to 255. If it is less than 0, it is set to 0.

You can use ColorTransform objects in the following ways:

  • As the colorTransform parameter of the colorTransform() method of the BitmapData class
  • As the colorTransform property of a Transform object (which can be used as the transform property of a display object)

Both approaches are included in application source but the second one is commented. The first approach lets you to specify rectangle area to which color transform will be applied.

ColorTransform methods

  • concat(second:ColorTransform):void – Concatenates the ColorTranform object specified by the second parameter with the current ColorTransform object and sets the current object as the result, which is an additive combination of the two color transformations. When you apply the concatenated ColorTransform object, the effect is the same as applying the second color transformation after the original color transformation.

References

Advertisement

Leave a Reply

Flexmaniaks on Facebook