Checking whether a color name is a valid color in Flex using the StyleManager class

The following example shows how to use the static StyleManager.isColorName() method to check whether a color name is valid or not. This method takes a single parameter, colorName, and returns a Boolean value representing whether the parameter is a valid alias for a color.

Full code after the jump.


<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<mx:Application xmlns:mx=""

            import mx.styles.StyleManager;
            import mx.utils.StringUtil;

            private function button_click(evt:MouseEvent):void {
                /* Remove leading and trailing whitespace. */
                var str:String = StringUtil.trim(textInput.text);
                var isColor:Boolean = StyleManager.isColorName(str);

                /* If it is a valid color, set the background color
                   and remove the error string, if any. Else, set the
                   background color to the constant NOT_A_COLOR from
                   the StyleManager class and set the error string. */
                if (isColor) {
                    box.setStyle("backgroundColor", str);
                    textInput.errorString = "";
                } else {
                    box.setStyle("backgroundColor", StyleManager.NOT_A_COLOR);
                    textInput.errorString = "NOT A COLOR";

    <mx:ApplicationControlBar dock="true">
        <mx:Label text="Color name:" />
        <mx:TextInput id="textInput" />
        <mx:Button label="isColorName()"
                click="button_click(event);" />

    <mx:Box id="box" width="100%" height="100%" />


View source is enabled in the following example.

If you want to add your own custom color aliases, you can use the StyleManager class’s registerColorName() method, as seen in the following snippet:

StyleManager.registerColorName('peter', 0x0BCDEF);
trace(StyleManager.isColorName('peter')); // true