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 !