Creating a linear gradient fill on an Ellipse object in Flex Gumbo

In a previous example, “Creating a solid color fill on an Ellipse object in Flex Gumbo”, we saw how you could create a solid color fill on an Ellipse object by setting the fill property to a SolidColor object.

The following example shows how you can create a linear gradient fill on a Flex Gumbo Ellipse object by setting the fill property to a LinearGradient object.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see “Using the beta Gumbo SDK in Flex Builder 3”.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/11/27/creating-a-linear-gradient-fill-on-an-ellipse-object-in-flex-gumbo/ -->
<FxApplication name="Ellipse_fill_LinearGradient_test"
        xmlns="http://ns.adobe.com/mxml/2009">
    <layout>
        <BasicLayout />
    </layout>

    <Form>
        <FormItem label="color 1:">
            <ColorPicker id="colorPicker1"
                    selectedColor="red" />
        </FormItem>
        <FormItem label="alpha 1:">
            <HSlider id="slider1"
                    minimum="0.0"
                    maximum="1.0"
                    value="1.0"
                    snapInterval="0.1"
                    tickInterval="0.1"
                    liveDragging="true" />
        </FormItem>
        <FormItem label="color 2:">
            <ColorPicker id="colorPicker2"
                    selectedColor="green" />
        </FormItem>
        <FormItem label="alpha 2:">
            <HSlider id="slider2"
                    minimum="0.0"
                    maximum="1.0"
                    value="1.0"
                    snapInterval="0.1"
                    tickInterval="0.1"
                    liveDragging="true" />
        </FormItem>
        <FormItem label="color 3:">
            <ColorPicker id="colorPicker3"
                    selectedColor="blue" />
        </FormItem>
        <FormItem label="alpha 3:">
            <HSlider id="slider3"
                    minimum="0.0"
                    maximum="1.0"
                    value="1.0"
                    snapInterval="0.1"
                    tickInterval="0.1"
                    liveDragging="true" />
        </FormItem>
    </Form>

    <Ellipse id="ellipse"
            width="200"
            height="300"
            horizontalCenter="100"
            verticalCenter="0">
        <fill>
            <LinearGradient>
                <GradientEntry id="gradientEntry1"
                        color="{colorPicker1.selectedColor}"
                        alpha="{slider1.value}" />
                <GradientEntry id="gradientEntry2"
                        color="{colorPicker2.selectedColor}"
                        alpha="{slider2.value}" />
                <GradientEntry id="gradientEntry3"
                        color="{colorPicker3.selectedColor}"
                        alpha="{slider3.value}" />
            </LinearGradient>
        </fill>
    </Ellipse>

</FxApplication>

View source is enabled in the following example.

You could also set the GradientEntry object’s color and alpha properties using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/11/27/creating-a-linear-gradient-fill-on-an-ellipse-object-in-flex-gumbo/ -->
<FxApplication name="Ellipse_fill_LinearGradient_test"
        xmlns="http://ns.adobe.com/mxml/2009">
    <layout>
        <BasicLayout />
    </layout>

    <Script>
        <![CDATA[
            import mx.events.ColorPickerEvent;
            import mx.events.SliderEvent;

            private function colorPicker1_change(evt:ColorPickerEvent):void {
                gradientEntry1.color = evt.color;
            }

            private function slider1_change(evt:SliderEvent):void {
                gradientEntry1.alpha = evt.value;
            }

            private function colorPicker2_change(evt:ColorPickerEvent):void {
                gradientEntry2.color = evt.color;
            }

            private function slider2_change(evt:SliderEvent):void {
                gradientEntry2.alpha = evt.value;
            }

            private function colorPicker3_change(evt:ColorPickerEvent):void {
                gradientEntry3.color = evt.color;
            }

            private function slider3_change(evt:SliderEvent):void {
                gradientEntry3.alpha = evt.value;
            }
        ]]>
    </Script>

    <Form>
        <FormItem label="color 1:">
            <ColorPicker id="colorPicker1"
                    selectedColor="red"
                    change="colorPicker1_change(event);" />
        </FormItem>
        <FormItem label="alpha 1:">
            <HSlider id="slider1"
                    minimum="0.0"
                    maximum="1.0"
                    value="1.0"
                    snapInterval="0.1"
                    tickInterval="0.1"
                    liveDragging="true"
                    change="slider1_change(event);" />
        </FormItem>
        <FormItem label="color 2:">
            <ColorPicker id="colorPicker2"
                    selectedColor="green"
                    change="colorPicker2_change(event);" />
        </FormItem>
        <FormItem label="alpha 2:">
            <HSlider id="slider2"
                    minimum="0.0"
                    maximum="1.0"
                    value="1.0"
                    snapInterval="0.1"
                    tickInterval="0.1"
                    liveDragging="true"
                    change="slider2_change(event);" />
        </FormItem>
        <FormItem label="color 3:">
            <ColorPicker id="colorPicker3"
                    selectedColor="blue"
                    change="colorPicker3_change(event);" />
        </FormItem>
        <FormItem label="alpha 3:">
            <HSlider id="slider3"
                    minimum="0.0"
                    maximum="1.0"
                    value="1.0"
                    snapInterval="0.1"
                    tickInterval="0.1"
                    liveDragging="true"
                    change="slider3_change(event);" />
        </FormItem>
    </Form>

    <Ellipse id="ellipse"
            width="200"
            height="300"
            horizontalCenter="100"
            verticalCenter="0">
        <fill>
            <LinearGradient>
                <GradientEntry id="gradientEntry1" color="red" />
                <GradientEntry id="gradientEntry2" color="green" />
                <GradientEntry id="gradientEntry3" color="blue" />
            </LinearGradient>
        </fill>
    </Ellipse>

</FxApplication>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/11/27/creating-a-linear-gradient-fill-on-an-ellipse-object-in-flex-gumbo/ -->
<FxApplication name="Ellipse_fill_LinearGradient_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        initialize="init();">
    <layout>
        <BasicLayout />
    </layout>

    <Script>
        <![CDATA[
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.ColorPicker;
            import mx.controls.HSlider;
            import mx.events.ColorPickerEvent;
            import mx.events.SliderEvent;
            import mx.graphics.Ellipse;
            import mx.graphics.GradientEntry;
            import mx.graphics.LinearGradient;

            private var colorPicker1:ColorPicker;
            private var colorPicker2:ColorPicker;
            private var colorPicker3:ColorPicker;
            private var slider1:HSlider;
            private var slider2:HSlider;
            private var slider3:HSlider;

            private var ellipse:Ellipse;
            private var gradientEntry1:GradientEntry;
            private var gradientEntry2:GradientEntry;
            private var gradientEntry3:GradientEntry;

            private function init():void {
                colorPicker1 = new ColorPicker();
                colorPicker1.selectedColor = 0xFF0000; // red
                colorPicker1.addEventListener(ColorPickerEvent.CHANGE, colorPicker1_change);

                colorPicker2 = new ColorPicker();
                colorPicker2.selectedColor = 0x00FF00; // green
                colorPicker2.addEventListener(ColorPickerEvent.CHANGE, colorPicker2_change);

                colorPicker3 = new ColorPicker();
                colorPicker3.selectedColor = 0x0000FF; // blue
                colorPicker3.addEventListener(ColorPickerEvent.CHANGE, colorPicker3_change);

                slider1 = new HSlider();
                slider1.minimum = 0.0;
                slider1.maximum = 1.0;
                slider1.value = 1.0;
                slider1.snapInterval = 0.1;
                slider1.tickInterval = 0.1;
                slider1.liveDragging = true;
                slider1.addEventListener(SliderEvent.CHANGE, slider1_change);

                slider2 = new HSlider();
                slider2.minimum = 0.0;
                slider2.maximum = 1.0;
                slider2.value = 1.0;
                slider2.snapInterval = 0.1;
                slider2.tickInterval = 0.1;
                slider2.liveDragging = true;
                slider2.addEventListener(SliderEvent.CHANGE, slider2_change);

                slider3 = new HSlider();
                slider3.minimum = 0.0;
                slider3.maximum = 1.0;
                slider3.value = 1.0;
                slider3.snapInterval = 0.1;
                slider3.tickInterval = 0.1;
                slider3.liveDragging = true;
                slider3.addEventListener(SliderEvent.CHANGE, slider3_change);

                var formItem1a:FormItem = new FormItem();
                formItem1a.label = "color 1:";
                formItem1a.addChild(colorPicker1);

                var formItem1b:FormItem = new FormItem();
                formItem1b.label = "alpha 1:";
                formItem1b.addChild(slider1);

                var formItem2a:FormItem = new FormItem();
                formItem2a.label = "color 2:";
                formItem2a.addChild(colorPicker2);

                var formItem2b:FormItem = new FormItem();
                formItem2b.label = "alpha 2:";
                formItem2b.addChild(slider2);

                var formItem3a:FormItem = new FormItem();
                formItem3a.label = "color 3:";
                formItem3a.addChild(colorPicker3);

                var formItem3b:FormItem = new FormItem();
                formItem3b.label = "alpha 3:";
                formItem3b.addChild(slider3);

                var form:Form = new Form();
                form.addChild(formItem1a); // colorPicker1
                form.addChild(formItem1b); // slider1
                form.addChild(formItem2a); // colorPicker2
                form.addChild(formItem2b); // slider2
                form.addChild(formItem3a); // colorPicker3
                form.addChild(formItem3b); // slider3
                addItem(form);

                gradientEntry1 = new GradientEntry(0xFF0000); // red
                gradientEntry2 = new GradientEntry(0x00FF00); // green
                gradientEntry3 = new GradientEntry(0x0000FF); // blue

                var gradientArr:Array = [];
                gradientArr.push(gradientEntry1);
                gradientArr.push(gradientEntry2);
                gradientArr.push(gradientEntry3);

                var linearGradient:LinearGradient = new LinearGradient();
                linearGradient.entries = gradientArr;

                ellipse = new Ellipse();
                ellipse.fill = linearGradient;
                ellipse.width = 200;
                ellipse.height = 300;
                ellipse.horizontalCenter = 100;
                ellipse.verticalCenter = 0;
                addItem(ellipse);
            }

            private function colorPicker1_change(evt:ColorPickerEvent):void {
                gradientEntry1.color = evt.color;
            }

            private function slider1_change(evt:SliderEvent):void {
                gradientEntry1.alpha = evt.value;
            }

            private function colorPicker2_change(evt:ColorPickerEvent):void {
                gradientEntry2.color = evt.color;
            }

            private function slider2_change(evt:SliderEvent):void {
                gradientEntry2.alpha = evt.value;
            }

            private function colorPicker3_change(evt:ColorPickerEvent):void {
                gradientEntry3.color = evt.color;
            }

            private function slider3_change(evt:SliderEvent):void {
                gradientEntry3.alpha = evt.value;
            }
        ]]>
    </Script>

</FxApplication>

This entry is based on a beta version of the Flex Gumbo 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 Gumbo SDK.