Flash Gallery | Flash Templates | Flash Menu | Flash Design | Flash Audio & Video

flash page flip

Actionscript 3.0 video tutorials

+ Reply to Thread
Results 1 to 1 of 1

Thread: ColorPicker - Flash CS3 Component

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,609
    Rep Power
    9

    ColorPicker - Flash CS3 Component

    flash templates
    Flash CS3 includes something new: the ColorPicker .
    Substantially it's a very clever component that allows the user to choose a colour from a well made palette.
    It's not a difficult component to manage, as the ColorPicker events and methods are quite self-explanatory.
    I've created a little application as example.
    Let's see in detail how it works'
    Here is the class I've written:
    Code:
    package
    {
    	import fl.controls.ColorPicker;
    	import fl.events.ColorPickerEvent;
    	import flash.geom.ColorTransform;
    	import flash.display.MovieClip;
    	import flash.display.Sprite;
    	import flash.events.Event;
    	import flash.events.MouseEvent;
    	import flash.geom.Point;
    	
    	public class Prova extends MovieClip
    	{
    		private var color_picker:ColorPicker;
    		
    		private var _color:ColorTransform;
    		
    		private var sprite:Sprite;
    		
    		private var clips_array:Array;
    		private var selectedClips_array:Array;
    		
    		private var startColor:Number=0x0066FF;
    		private var startX:Number;
    		private var startY:Number;
    		
    		private var point:Point;
    		
    		private var isOpen:Boolean=false;
    		
    		public function set col(c:Number):void
    		{
    			_color.color=c;
    		}
    		public function get col():Number
    		{
    			return(_color.color);
    		}
    
    		public function Prova()
    		{
    			init();
    			initStartClipColors();
    			initListeners();
    		}
    		
    		private function init():void
    		{
    			stage.frameRate=31;
    			
    			clips_array=new Array(clip_0_mc,clip_1_mc,clip_2_mc,clip_3_mc);
    			selectedClips_array=new Array();
    			color_picker=new ColorPicker();
    			_color=new ColorTransform();
    			col=startColor;
    			addChild(color_picker);
    			
    			color_picker.x=100;
    			color_picker.y=300;
    		}
    		
    		private function initStartClipColors():void
    		{
    			for(var i:int=0;i < clips_array.length;i++)
    			{
    				clips_array[i].transform.colorTransform=_color;
    			}
    		}
    		
    		private function initListeners():void
    		{
    			color_picker.addEventListener(ColorPickerEvent.CHANGE,settaColore);
    			stage.addEventListener(MouseEvent.MOUSE_DOWN,azionaMouse);
    			stage.addEventListener(MouseEvent.MOUSE_UP,stoppaMouse);
    		}
    		
    		private function settaColore(c:ColorPickerEvent):void
    		{
    			isOpen=false;
    			_color=new ColorTransform();
    			col=c.target.selectedColor;
    			for(var i:int=0;i < selectedClips_array.length;i++)
    			{
    				
    				selectedClips_array[i].transform.colorTransform=_color;
    			}
    		}
    		
    		private function azionaMouse(m:MouseEvent):void
    		{
    			startX=mouseX;
    			startY=mouseY;
    			
    			point=new Point(mouseX,mouseY);
    			if(!color_picker.hitTestPoint(point.x,point.y,true))
    			{
    				aggiungiAlpha();
    			
    				sprite=new Sprite();
    				addChild(sprite);
    				
    				addEventListener(Event.ENTER_FRAME,drawing);
    			}
    		}
    		
    		private function drawing(e:Event):void
    		{
    			sprite.graphics.clear();
    			sprite.graphics.moveTo(startX,startY);
    			sprite.graphics.lineStyle(1,0xCCCCCC,1);
    			sprite.graphics.drawRect(startX,startY,mouseX-startX,mouseY-startY);
    		}
    		
    		private function stoppaMouse(m:MouseEvent):void
    		{
    			removeEventListener(Event.ENTER_FRAME,drawing);
    			if(!color_picker.hitTestPoint(point.x,point.y,true)&&!isOpen)
    			{
    				selectedClips_array=new Array();
    				for(var i:int=0;i < clips_array.length;i++)
    				{
    					if(sprite.hitTestObject(clips_array[i]))
    					{
    						selectedClips_array.push(clips_array[i]);
    						togliAlpha(clips_array[i]);
    					}
    				}
    				removeChild(sprite);
    			}
    			else
    				isOpen=true;
    		}
    		
    		private function togliAlpha(m:MovieClip):void
    		{
    			m.alpha=.5;
    		}
    		
    		private function aggiungiAlpha():void
    		{
    			for(var i:int=0;i < selectedClips_array.length;i++)
    			{
    				selectedClips_array[i].alpha=1;
    			}
    		}
    	}
    }
    Here is the result:







    Apart from some conditional logic, I'd like to draw the attention to a couple of points.
    The first one is the getter/setter used in this part:
    Code:
    private var _color:ColorTransform;
    public function set col(c:Number):void
    {
    	_color.color=c;
    }
    public function get col():Number
    {
    	return(_color.color);
    }
    Normally I'd have used the getter/setter like so:
    Code:
    private var _color:ColorTransform;
    public function set col(c:ColorTransform):void
    {
    	_color=c;
    }
    public function get col():ColorTransform
    {
    	return(_color);
    }
    But because I needed the colour property of the ColorTransform instance (_color), I've opted to encapsulate the actual instance's property and not the instance itself.

    The second point is the following:
    Code:
    private function initListeners():void
    {
    	color_picker.addEventListener(ColorPickerEvent.CHANGE,settaColore);
    	stage.addEventListener(MouseEvent.MOUSE_DOWN,azionaMouse);
    	stage.addEventListener(MouseEvent.MOUSE_UP,stoppaMouse);
    }
    
    private function settaColore(c:ColorPickerEvent):void
    {
    	isOpen=false;
    	_color=new ColorTransform();
    	col=c.target.selectedColor;
    	for(var i:int=0;i < selectedClips_array.length;i++)
    	{
    		
    		selectedClips_array[i].transform.colorTransform=_color;
    	}
    }
    Apart from the 2 listeners watching the mouse events, there's another listener watching the ColorPicker and the event (CHANGE of the ColorPickerEvent class) is dispatched when the user clicks a color on the component's color panel.
    That's it,! A hexadecimal value is then obtained, which I can use whichever way I like.

    Stay tuned !

+ Reply to Thread

Similar Threads

  1. problema popolamento dinamico colorpicker
    By nextframe in forum Actionscript 3.0 avanzato
    Replies: 2
    Last Post: 18-06-10, 13:43
  2. Replies: 89
    Last Post: 01-05-10, 15:02
  3. Componente ColorPicker Flash CS3
    By Flep in forum Articoli e tutorials
    Replies: 1
    Last Post: 20-03-09, 11:35
  4. RadioButton component of Flash CS3
    By Flep in forum Tutorials
    Replies: 1
    Last Post: 05-08-08, 08:52
  5. Using NumericStepper - Flash CS3 component
    By Flep in forum Tutorials
    Replies: 1
    Last Post: 09-02-08, 17:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts