Recipe 10.12. Changing Saturation


Problem

You want to change the saturation of a display object.

Solution

Use a saturation matrix and apply it with a ColorMatrixFilter object.

Discussion

You can affect saturation with the following matrix:

a  b  c  0  0 d  e  f  0  0 g  h  i  0  0 0  0  0  1  0

In the preceding matrix, you need to determine the values of a through i with the following equations, where red, blue, and green are the luminance constants and value is the saturation value:

a = (1  value) * red + value b = (1  value) * green c = (1  value) * blue d = (1  value) * red e = (1  value) * green + value f = (1  value) * blue g = (1  value) * red h = (1  value) * green i = (1  value) * blue + value

When the saturation value is 0, the matrix is a grayscale matrix.

You can use the ascb.filters.ColorMatrixArrays.getSaturationArray( ) method to construct a saturation matrix array, given just the value.

sampleSprite.filters = [new ColorMatrixFilter(ColorMatrixArrays.getSaturationArray(2))];




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net