com.neolao.components.ColorPanel

Composant pour choisir une couleur.

Classes recquises:

Propriétés

Visibilité Type Nom Status Description
public Number color Lecture et écriture La couleur courante

Méthodes

Visibilité Return Nom Param 1 Description
public Boolean addTarget pMC:MovieClip Ajoute une cible
public Boolean removeTarget pMC:MovieClip Retire une cible
public Boolean removeAllTarget Retire toutes les cibles

Evénements

Nom Param 1 Description
onChanged pColor:Number Invoqué lorsqu'une nouvelle couleur est choisie. La couleur est passée en paramètre.
onChanging pColor:Number Invoqué pendant le choix d'une couleur. La couleur est passée en paramètre.

Source

import com.neolao.controls.Delegate;
[IconFile("ColorPanel.png")]
/**
 * Composant pour choisir une couleur
 * @author 	neolao <neo@neolao.com>
 * @version	1.0 (29/06/2005)
 * @link	http://resources.neolao.com/flash/classes/components/ColorPanel
 * @license	http://creativecommons.org/licenses/by-sa/2.0/
 */
dynamic class com.neolao.components.ColorPanel extends MovieClip{
	public var className:String = "ColorPanel";
	public var version:String = "1.0";
 
	// ----------------------------- CONSTANTES --------------------------------
	/**
	 * Largeur de la palette de couleurs
	 */
	private var COLORS_WIDTH:Number = 100;
	/**
	 * Hauteur de la palette de couleurs
	 */
	private var COLORS_HEIGHT:Number = 50;
	/**
	 * Largeur de la palette noir & blanc
	 */
	private var BLACKWHITE_WIDTH:Number = 10;
	/**
	 * Hauteur de la palette noir & blanc
	 */
	private var BLACKWHITE_HEIGHT:Number = 50;
	/**
	 * Décalage de l'apercu de la couleur
	 */
	private var PREVIEW_OFFSET:Number = 5;
 
	// ----------------------------- VARIABLES ---------------------------------
	/**
	 * La couleur courante
	 */
	private var _color:Number;
	/**
	 * Ecouteur pour le curseur
	 */
	private var _cursorListener:Object;
	/**
	 * L'objet Color de l'aperçu
	 */
	private var _previewColor:Color;
	/**
	 * Le MovieClip à modifier
	 */
	private var _targets:Array;
 
	// ----------------------------- EVENEMENTS --------------------------------
	/**
	 * Invoqué lorsqu'une nouvelle couleur est choisie
	 * @param pColor:Number La nouvelle couleur en hexadécimal
	 */
	public var onChanged:Function;
	/**
	 * Invoqué pendant le choix d'une couleur
	 * @param pColor:Number La nouvelle couleur en hexadécimal
	 */
	public var onChanging:Function;
 
	/*============================= CONSTRUCTEUR =============================*/
	/*========================================================================*/
	/**
	 * Initialisation du composant
	 */
	public function ColorPanel(){
		_initTargets();	
		_initCursor();
		_initPreview();
		_color = 0xffffff;
	}
	/*======================= FIN = CONSTRUCTEUR = FIN =======================*/
	/*========================================================================*/
 
	/*=========================== METHODES PRIVEES ===========================*/
	/*========================================================================*/
	/**
	 * Initialisation du curseur
	 */
	private function _initCursor(){
		this.cursor_mc.onPress = Delegate.create(this, _cursorPress);
		this.cursor_mc.onRelease = this.cursor_mc.onReleaseOutside = Delegate.create(this, _cursorRelease);
		_cursorListener = new Object();
		_cursorListener.onMouseMove = Delegate.create(this, _cursorMove);		
 
		this.cursor_mc._x = 0;
		this.cursor_mc._y = 0;
	}
	/**
	 * onPress du curseur
	 */
	private function _cursorPress(){
		Mouse.addListener(_cursorListener);
		Mouse.hide();
	}
	/**
	 * onRelease du curseur
	 */
	private function _cursorRelease(){
		Mouse.removeListener(_cursorListener);
		Mouse.show();
		_positionToColor();
		_setColor();
		onChanged(_color);
	}
	/**
	 * onMouseMove du curseur
	 * @param pX L'abscisse du curseur
	 * @param pY L'ordonnée du curseur
	 */
	private function _cursorMove(pX:Number, pY:Number){
		var vCursorX:Number = (pX != undefined)?pX:this._xmouse;
		var vCursorY:Number = (pY != undefined)?pY:this._ymouse;
		var vPreviewX:Number;
		var vPreviewY:Number;
 
		// Curseur X
		if(vCursorX < 0){
			vCursorX = 0;
		}
		if(vCursorX > COLORS_WIDTH + BLACKWHITE_WIDTH){
			vCursorX = COLORS_WIDTH + BLACKWHITE_WIDTH;
		}
		// Curseur Y
		if(vCursorY < 0){
			vCursorY = 0;
		}
		if(vCursorY > COLORS_HEIGHT){
			vCursorY = COLORS_HEIGHT;
		}
		// Curseur position
		this.cursor_mc._x = vCursorX;
		this.cursor_mc._y = vCursorY;
 
		// Preview X
		vPreviewX = vCursorX + PREVIEW_OFFSET;
		if(vCursorX < this.preview_mc._width + PREVIEW_OFFSET){
			vPreviewX = vCursorX + PREVIEW_OFFSET;
		}
		if(vCursorX > COLORS_WIDTH + BLACKWHITE_WIDTH - this.preview_mc._width - PREVIEW_OFFSET){
			vPreviewX = vCursorX - this.preview_mc._width - PREVIEW_OFFSET;
		}
		// Preview Y
		vPreviewY = vCursorY + PREVIEW_OFFSET;
		if(vCursorY < this.preview_mc._height + PREVIEW_OFFSET){
			vPreviewY = vCursorY + PREVIEW_OFFSET;
		}
		if(vCursorY > COLORS_HEIGHT - this.preview_mc._height - PREVIEW_OFFSET){
			vPreviewY = vCursorY - this.preview_mc._height - PREVIEW_OFFSET;
		}
		// Preview position
		this.preview_mc._x = vPreviewX;
		this.preview_mc._y = vPreviewY;
 
		updateAfterEvent();
		_positionToColor();
		onChanging(_color);
	}
	/**
	 * Initialisation de l'aperçu
	 */
	private function _initPreview(){
		_previewColor = new Color(this.preview_mc.zone_mc);
 
		this.preview_mc._x = PREVIEW_OFFSET;
		this.preview_mc._y = PREVIEW_OFFSET;
	}
	/**
	 * Converti les coordonnées du curseur en couleur
	 * _x modifie la teinte
	 * _y modifie la luminosité
	 */
	private function _positionToColor(){
		var vTransitionWidth:Number = COLORS_WIDTH / 6;
		var vCursorX:Number = this.cursor_mc._x;
		var vCursorY:Number = this.cursor_mc._y;
		var vColor:Object = new Object();
		var vBrightness:Number;
		var vBlackWhite:Boolean = false;
 
		// Teinte
		vColor.ra = 0;
		vColor.ga = 0;
		vColor.ba = 0;
		vColor.aa = 100;
		vColor.ab = 0;
		if(vCursorX < vTransitionWidth){
			// tranche FF0000 vers FFFF00
			vColor.rb = 255;
			vColor.gb = vCursorX * 255 / vTransitionWidth;
			vColor.bb = 0;
		}else if(vCursorX < vTransitionWidth*2){
			// tranche FFFF00 vers 00FF00
			vColor.rb = 255 - ((vCursorX - vTransitionWidth) * 255 / vTransitionWidth);
			vColor.gb = 255;
			vColor.bb = 0;
		}else if(vCursorX < vTransitionWidth*3){
			// tranche 00FF00 vers 00FFFF
			vColor.rb = 0;
			vColor.gb = 255;
			vColor.bb = (vCursorX - vTransitionWidth*2) * 255 / vTransitionWidth;
		}else if(vCursorX < vTransitionWidth*4){
			// tranche 00FFFF vers 0000FF
			vColor.rb = 0;
			vColor.gb = 255 - ((vCursorX - vTransitionWidth*3) * 255 / vTransitionWidth);
			vColor.bb = 255;
		}else if(vCursorX < vTransitionWidth*5){
			// tranche 0000FF vers FF00FF
			vColor.rb = (vCursorX - vTransitionWidth*4) * 255 / vTransitionWidth;
			vColor.gb = 0;
			vColor.bb = 255;
		}else if(vCursorX < vTransitionWidth*6){
			// tranche FF00FF vers FF0000
			vColor.rb = 255;
			vColor.gb = 0;
			vColor.bb = 255 - ((vCursorX - vTransitionWidth*5) * 255 / vTransitionWidth);
		}else{
			// Noir & blanc
			vColor.rb = 255;
			vColor.gb = 255;
			vColor.bb = 255;
			vBlackWhite = true;
		}
 
		// Luminosité
		if(vBlackWhite){
			vBrightness = 255 * vCursorY / BLACKWHITE_HEIGHT;
		}else{
			//vLum = (vCursorY - COLORS_HEIGHT / 2) * (255 / (COLORS_HEIGHT / 2));
			vBrightness = 510 * vCursorY / COLORS_HEIGHT - 255;
		}
		vColor.rb -= vBrightness;
		vColor.gb -= vBrightness;
		vColor.bb -= vBrightness;
 
		if(vColor.rb > 255){
			vColor.rb = 255;
		}
		if(vColor.rb < 0){
			vColor.rb = 0;
		}
		if(vColor.gb > 255){
			vColor.gb = 255;
		}
		if(vColor.gb < 0){
			vColor.gb = 0;
		}
		if(vColor.bb > 255){
			vColor.bb = 255;
		}
		if(vColor.bb < 0){
			vColor.bb = 0;
		}
 
		_previewColor.setTransform(vColor);
		_color = _previewColor.getRGB();
	}
	/**
	 * Converti la couleur en coordonnées pour le curseur curseur
	 */
	private function _colorToPosition(){
		var vTransitionWidth:Number = COLORS_WIDTH / 6;
		var vTransform:Object = _previewColor.getTransform();
		var vRed:Number = vTransform.rb;
		var vGreen:Number = vTransform.gb;
		var vBlue:Number = vTransform.bb;
		var vTintRed:Number;
		var vTintGreen:Number;
		var vTintBlue:Number;
		var vMax:Number = vRed;
		var vMin:Number = vRed;
		var vBrightness:Number;
		var vCursorX:Number;
		var vCursorY:Number;
		var vBlackWhite:Boolean = false;;		
 
		// Il faut trouver le plus grand et le plus petit des 3
		if(vGreen > vMax){
			vMax = vGreen;
		}
		if(vGreen < vMin){
			vMin = vGreen;
		}
		if(vBlue > vMax){
			vMax = vBlue;
		}
		if(vBlue < vMin){
			vMin = vBlue;
		}
 
		// On trouve la teinte grace au rapport du milieu sur le plus grand
		if(vRed == vMax && vGreen == vMax && vBlue == vMax){
			// Noir & Blanc
			vCursorX = COLORS_WIDTH + BLACKWHITE_WIDTH / 2;
			vBlackWhite = true;
		}else if(vRed == vMax && vBlue == vMin){
			// tranche FF0000 vers FFFF00
			// on monte
			vTintRed = 255;
			vTintGreen = (vGreen - vMin) * 255 / (vMax - vMin);
			vTintBlue = 0;
			vCursorX = vTintGreen * vTransitionWidth / 255;
		}else if(vGreen == vMax && vBlue == vMin){
			// tranche FFFF00 vers 00FF00
			// on descend
			vTintRed = (vRed - vMin) * 255 / (vMax - vMin);
			vTintGreen = 255;
			vTintBlue = 0;
			vCursorX = (255 - vTintRed) * vTransitionWidth / 255 + vTransitionWidth;
		}else if(vGreen == vMax && vRed == vMin){
			// tranche 00FF00 vers 00FFFF
			// on monte
			vTintRed = 0;
			vTintGreen = 255;
			vTintBlue = (vBlue - vMin) * 255 / (vMax - vMin);
			vCursorX = vTintBlue * vTransitionWidth / 255 + vTransitionWidth*2;
		}else if(vBlue == vMax && vRed == vMin){
			// tranche 00FFFF vers 0000FF
			// on descend
			vTintRed = 0;
			vTintGreen = (vGreen - vMin) * 255 / (vMax - vMin);
			vTintBlue = 255;
			vCursorX = (255 - vTintGreen) * vTransitionWidth / 255 + vTransitionWidth*3;
		}else if(vBlue == vMax && vGreen == vMin){
			// tranche 0000FF vers FF00FF
			// on monte
			vTintRed = (vRed - vMin) * 255 / (vMax - vMin);
			vTintGreen = 0;
			vTintBlue = 255;
			vCursorX = vTintRed * vTransitionWidth / 255 + vTransitionWidth*4;
		}else{
		//}else if(vRed == vMax && vGreen == vMin){
			// tranche FF00FF vers FF0000
			// on descend
			vTintRed = 255;
			vTintGreen = 0;
			vTintBlue = (vBlue - vMin) * 255 / (vMax - vMin);
			vCursorX = (255 - vTintBlue) * vTransitionWidth / 255 + vTransitionWidth*5;
		}
 
		// Luminosité
		if(vBlackWhite){
			vBrightness = vMax;
		}else if(vMin == 0){
			// La luminosité est négative
			vBrightness = -255 + vMax;
		}else{
			// La luminosité est positive
			vBrightness = vMin;
		}
 
		if(vBlackWhite){
			vCursorY = BLACKWHITE_HEIGHT - BLACKWHITE_HEIGHT * vBrightness / 255;
		}else{
			vCursorY = COLORS_HEIGHT / 2 - (COLORS_HEIGHT / 2) * vBrightness / 255;
		}
 
		_cursorMove(vCursorX, vCursorY);
		_setColor();
		onChanged(_color);
	}
	/**
	 * Initialisation des cibles
	 */
	private function _initTargets(){
		_targets = new Array();
	}
	/**
	 * Change la couleurs des cibles
	 */
	private function _setColor(){
		var vColor:Color;
 
		for(var i=0; i<_targets.length; i++){
			vColor = new Color(_targets[i]);
			vColor.setRGB(_color);
		}
	}
	/*===================== FIN = METHODES PRIVEES = FIN =====================*/
	/*========================================================================*/
 
	/*============================ GETTER  SETTER ============================*/
	/*========================================================================*/
	/**
	 * La couleur courante
	 */
	public function get color():Number {
		return _color;
	}
	public function set color(pColor:Number){
		_color = pColor;
		_previewColor.setRGB(pColor);
		_colorToPosition();
	}
	/*====================== FIN = GETTER  SETTER = FIN ======================*/
	/*========================================================================*/
 
	/*========================== METHODES PUBLIQUES ==========================*/
	/*========================================================================*/
	/**
	 * Ajoute une cible
	 * @param pMC Le MovieClip à modifier
	 * @return true si le MovieClip a bien été ajouté, sinon false parce qu'il existe déjà dans la liste
	 */
	public function addTarget(pMC:MovieClip):Boolean{
		for(var i=0; i<_targets.length; i++){
			if(_targets[i] == pMC){
				return false;	
			}
		}
		_targets.push(pMC);
		return true;
	}
	/**
	 * Retire une cible
	 * @param pMC Le MovieClip à retirer
	 * @return true si le MovieClip a bien été retiré, sinon false parce qu'il n'existe pas dans la liste
	 */
	public function removeTarget(pMC:MovieClip):Boolean {
		for(var i=0; i<_targets.length; i++){
			if(_targets[i] == pMC){
				_targets.splice(i, 1);
				return true;	
			}
		}
		return false;
	}
	/**
	 * Retire toutes les cibles
	 * @return true
	 */
	public function removeAllTarget():Boolean {
		_targets = new Array();
		return true;
	}
	/*==================== FIN = METHODES PUBLIQUES = FIN ====================*/
	/*========================================================================*/
}