The following example shows how you can convert a color from it’s HSB (Hue/Saturation/Brightness) format to standard RGB (Red/Green/Blue) format using the static HSBColor.convertHSBtoRGB() method.

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 http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.
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"?>
<!-- http://blog.flexexamples.com/2009/10/17/converting-colors-from-hsb-to-rgb-format-in-flex-4/ -->
<s:Application name="Spark_HSBColor_convertHSBtoRGB_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
    <s:controlBarContent>
        <mx:Form>
            <mx:FormItem label="hue:">
                <s:HSlider id="hueSl"
                        minimum="0"
                        maximum="360"
                        valueCommit="setColor();" />
            </mx:FormItem>
            <mx:FormItem label="saturation:">
                <s:HSlider id="saturationSl"
                        minimum="0.0"
                        maximum="1.0"
                        value="1.0"
                        snapInterval="0.01"
                        stepSize="0.01"
                        valueCommit="setColor();" />
            </mx:FormItem>
            <mx:FormItem label="brightness:">
                <s:HSlider id="brightnessSl"
                        minimum="0.0"
                        maximum="1.0"
                        value="1.0"
                        snapInterval="0.01"
                        stepSize="0.01"
                        valueCommit="setColor();" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.utils.HSBColor;
 
            private function setColor():void {
                brdr.setStyle("backgroundColor", rgbColor);
            }
 
            protected function formatRGB(value:uint):String {
                var str:String = String("000000" + value.toString(16)).substr(-6);
                return "#" + str.toUpperCase();
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <fx:uint id="rgbColor">{HSBColor.convertHSBtoRGB(hueSl.value, saturationSl.value, brightnessSl.value)}</fx:uint>
    </fx:Declarations>
 
    <s:Border id="brdr"
            left="20" right="20" top="20" bottom="20">
        <s:Label id="lbl"
                text="{formatRGB(rgbColor)}"
                fontSize="96"
                blendMode="invert"
                horizontalCenter="0" verticalCenter="0" />
    </s:Border>
 
</s:Application>

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.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

One Response to Converting colors from HSB to RGB format in Flex 4

  1. Tahir Alvi says:

    Very nice work.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree