Modifying a color’s brightness in Flex 4

by Peter deHaan on October 14, 2009

in HSBColor (Spark), beta2

The following example shows how you can modify a color’s brightness in Flex 4 by using the static HSBColor.convertRGBtoHSB() and HSBColor.convertHSBtoRGB() methods and the brightness 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 beta, check out the Adobe Flash Builder 4 page on the Adobe Labs site. To download the latest build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4. For instructions on using the beta Flex 4 SDK in Flex Builder 3, see "Using the beta Flex 4 SDK in Flex Builder 3".

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/10/14/modifying-a-colors-brightness-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"
        creationComplete="setBrightness();">
    <s:controlBarContent>
        <mx:Form backgroundAlpha="0.0" paddingTop="0" paddingBottom="0">
            <mx:FormItem label="color:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="red"
                        change="setBrightness();" />
            </mx:FormItem>
            <mx:FormItem label="brightness:">
                <s:HSlider id="slider"
                        minimum="0.0" maximum="1.0"
                        value="1.0"
                        snapInterval="0.01" stepSize="0.01"
                        valueCommit="setBrightness();" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.utils.HSBColor;
 
            protected function modifyBrightness(color:uint, brightness:Number):uint {
                var hsbColor:HSBColor = HSBColor.convertRGBtoHSB(color);
                hsbColor.brightness = brightness;
                return HSBColor.convertHSBtoRGB(hsbColor.hue, hsbColor.saturation, hsbColor.brightness);
            }
 
            protected function setBrightness():void {
                fillCol.color = modifyBrightness(colorPicker.selectedColor, slider.value);
            }
 
            protected function formatRGB(value:uint):String {
                var str:String = String("000000" + value.toString(16)).substr(-6);
                return "#" + str.toUpperCase();
            }
        ]]>
    </fx:Script>
 
    <s:Rect id="rect"
            left="50" right="50" top="50" bottom="50">
        <s:fill>
            <s:SolidColor id="fillCol" />
        </s:fill>
    </s:Rect>
 
    <s:Label id="lbl"
            text="{formatRGB(fillCol.color)}"
            horizontalCenter="0" bottom="10" />
 
</s:Application>

View source is enabled in the following example.

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.

{ 1 comment… read it below or add one }

1 Anonymous November 4, 2009 at 5:29 am

thanks, very nice work!

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: