Modifying a color’s saturation in Flex 4

In a previous example, “Modifying a color’s brightness in Flex 4”, we saw how you could modify a color’s brightness in Flex 4 by using the static HSBColor.convertRGBtoHSB() and HSBColor.convertHSBtoRGB() methods and the brightness property.

The following example shows how you can modify a color’s saturation in Flex 4 by using the static HSBColor.convertRGBtoHSB() and HSBColor.convertHSBtoRGB() methods and the saturation property.

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see To download the latest nightly build of the Flex 4 SDK, see

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<s:Application name="Spark_HSBColor_saturation_test"
        <mx:Form backgroundAlpha="0.0" paddingTop="0" paddingBottom="0">
            <mx:FormItem label="color:">
                <mx:ColorPicker id="colorPicker"
                        change="setSaturation();" />
            <mx:FormItem label="saturation:">
                <s:HSlider id="slider"
                        minimum="0.0" maximum="1.0"
                        snapInterval="0.01" stepSize="0.01"
                        valueCommit="setSaturation();" />
            import mx.utils.HSBColor;
            protected function modifySaturation(color:uint, saturation:Number):uint {
                var hsbColor:HSBColor = HSBColor.convertRGBtoHSB(color);
                hsbColor.saturation = saturation;
                return HSBColor.convertHSBtoRGB(hsbColor.hue, hsbColor.saturation, hsbColor.brightness);
            protected function setSaturation():void {
                fillCol.color = modifySaturation(colorPicker.selectedColor, slider.value);
            protected function formatRGB(value:uint):String {
                var str:String = String("000000" + value.toString(16)).substr(-6);
                return "#" + str.toUpperCase();
    <s:Rect id="rect"
            left="50" right="50" top="50" bottom="50">
            <s:SolidColor id="fillCol" />
    <s:Label id="lbl"
            horizontalCenter="0" bottom="10" />

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.