/** * ColorChooser.as * Keith Peters * version 0.97 * * A bare bones Color Chooser component, allowing for textual input only. * * Copyright (c) 2009 Keith Peters * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.bit101.components { import flash.display.DisplayObjectContainer; import flash.display.Sprite; import flash.events.Event; public class ColorChooser extends Component { private var _input:InputText; private var _swatch:Sprite; private var _value:uint = 0xff0000; /** * Constructor * @param parent The parent DisplayObjectContainer on which to add this ColorChooser. * @param xpos The x position to place this component. * @param ypos The y position to place this component. * @param value The initial color value of this component. * @param defaultHandler The event handling function to handle the default event for this component (change in this case). */ public function ColorChooser(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, value:uint = 0xff0000, defaultHandler:Function = null) { _value = value; super(parent, xpos, ypos); if(defaultHandler != null) { addEventListener(Event.CHANGE, defaultHandler); } } /** * Initializes the component. */ override protected function init():void { super.init(); _width = 65; _height = 15; value = _value; } override protected function addChildren():void { _input = new InputText(); _input.width = 45; _input.restrict = "0123456789ABCDEFabcdef"; _input.maxChars = 6; addChild(_input); _input.addEventListener(Event.CHANGE, onChange); _swatch = new Sprite(); _swatch.x = 50; _swatch.filters = [getShadow(2, true)]; addChild(_swatch); } /////////////////////////////////// // public methods /////////////////////////////////// /** * Draws the visual ui of the component. */ override public function draw():void { super.draw(); _swatch.graphics.clear(); _swatch.graphics.beginFill(_value); _swatch.graphics.drawRect(0, 0, 16, 16); _swatch.graphics.endFill(); } /////////////////////////////////// // event handlers /////////////////////////////////// /** * Internal change handler. * @param event The Event passed by the system. */ protected function onChange(event:Event):void { event.stopImmediatePropagation(); _value = parseInt("0x" + _input.text, 16); _input.text = _input.text.toUpperCase(); invalidate(); dispatchEvent(new Event(Event.CHANGE)); } /////////////////////////////////// // getter/setters /////////////////////////////////// /** * Gets / sets the color value of this ColorChooser. */ public function set value(n:uint):void { var str:String = n.toString(16).toUpperCase(); while(str.length < 6) { str = "0" + str; } _input.text = str; _value = parseInt("0x" + _input.text, 16); invalidate(); } public function get value():uint { return _value; } } }