color palette demo
color palette demo
@author naoto koshikawa
package
{
import caurina.transitions.Tweener;
import flash.display.Shape;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
[SWF(width = "465", height = "465", backgroundColor = "0xEEEEE3", frameRate = "30")]
/**
* color palette demo
* @author naoto koshikawa
*/
public class Demo4 extends Sprite
{
private static const CELL_SIZE:Number = 20;
private static const COLMUN_COUNT:Number = 3;
private static const CELL_MARGIN:Number = 1;
public function Demo4()
{
var text:TextField = new TextField();
text.x = text.y = 0;
text.autoSize = TextFieldAutoSize.LEFT;
text.defaultTextFormat = new TextFormat("Helvetica", 42, 0xCC2200);
text.text = "Color Palette Demo";
addChild(text);
text.x = text.y = 20;
var generator:ColorGenerator = new ColorGenerator(0x33);
var colors:Array = generator.colors;
var count:uint = generator.eachCount;
var size:uint = CELL_SIZE;
var colorList:Array = [];
var columnCount:uint = COLMUN_COUNT;
var margin:uint = CELL_MARGIN;
var container:Sprite = new Sprite();
container.graphics.beginFill(0x000000);
container.graphics.drawRect(0, 0, count * (size + margin) * columnCount - margin, count * (size + margin) * count / columnCount - margin);
container.x = stage.stageWidth / 2 - container.width / 2;
container.y = stage.stageHeight / 2 - container.height / 2;
addChild(container);
for (var r:uint = 0; r<count; r++)
{
for (var g:uint = 0; g<count; g++)
{
for (var b:uint = 0; b<count; b++)
{
var cellX:uint = r * (size + margin) * count
+ g * (size + margin)
- Math.floor(r / columnCount) * (size + margin) * count * columnCount;
var cellY:uint = Math.floor(r / columnCount) * (size + margin) * count
+ b * (size + margin);
var cell:Shape = new Shape();
cell.graphics.beginFill(colors[r][g][b], 1.0);
cell.graphics.drawRect(0, 0, size, size);
cell.x = cellX;
cell.y = cellY;
cell.alpha = 0;
colorList.push(container.addChild(cell));
}
}
}
for (var i:uint = 0; i < colorList.length; i++)
{
var xposition:Number = colorList[i].x;
var yposition:Number = colorList[i].y;
firstTweener(colorList[i], { y:0, xposition:xposition, yposition:yposition, alpha:1, i:i } );
}
}
private function firstTweener(obj:Shape, prop:Object):void
{
Tweener.addTween(obj, {
alpha:prop.alpha, y:prop.y,
onComplete:nextTweener,
onCompleteParams:[obj, {xposition:prop.xposition, yposition:prop.yposition, i:prop.i}],
delay:prop.i / 200, time:2.0,
transition:"easeOutBounce" } );
}
private function nextTweener(obj:Shape, prop:Object):void
{
Tweener.addTween(obj, {
x:prop.xposition,
y:prop.yposition,
onComplete:firstTweener,
onCompleteParams:[obj, { y:0, xposition:prop.xposition, yposition:prop.yposition, alpha:1, i:prop.i }],
delay:Math.random(), time:5,
transition:"easeOutExpo" } );
}
}
}
/**
* color generator
* @author naoto koshikawa
*/
class ColorGenerator
{
/** error message */
private static const ERROR_MESSAGE:String = "arguments error";
/** rgb element max */
private static const RGB_ELEMENT_MAX:uint = 0xFF;
private var _colors:Array;
/** generated colors */
public function get colors():Array
{
return _colors;
}
/** element step */
private var _step:uint;
private var _eachCount:uint;
/** each count per color element */
public function get eachCount():uint
{
return _eachCount;
}
/** color count */
private var _count:uint;
/**
* constructor
* @param step step generated colors
*/
public function ColorGenerator(step:uint)
{
if (step == 0) throw(ArgumentError(ERROR_MESSAGE));
if (RGB_ELEMENT_MAX < step) throw(ArgumentError(ERROR_MESSAGE));
if (RGB_ELEMENT_MAX % step != 0) throw(ArgumentError(ERROR_MESSAGE));
_step = step;
_eachCount = RGB_ELEMENT_MAX / _step + 1;
_count = _eachCount * _eachCount * _eachCount; // R colors * G colors * B colors
generate();
}
/** generate colors */
private function generate():void
{
_colors = [];
for (var r:uint = 0; r < _eachCount; r++)
{
_colors[r] = [];
for (var g:uint = 0; g < _eachCount; g++)
{
_colors[r][g] = [];
for (var b:uint = 0; b < _eachCount; b++)
{
_colors[r][g][b] = _step * r << 16 | _step * g << 8 | _step * b;
}
}
}
}
}
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
/**
* arrange DisplayObject lika a tile
* @author naoto koshikawa
* @param displayList
* @param turnCount
* @param direction 0:horizon, 1:vertical
* @param horizontalMargin
* @param vertialMargin
* @param centering
*/
function arrangeDisplayObject(displayList:Array, turnCount:Number = 5, direction:uint = 0,
horizontalMargin:Number = 0, verticalMargin:Number = 0, centering:Boolean = false):void
{
if (!turnCount) throw(new ArgumentError("turnCount(2nd arguments) must be more then 1"));
var i:Number = 0;
var maxWidth:Number = 0;
var maxHeight:Number = 0;
maxWidth = Math.max.apply(null, displayList.map(getWidth));
maxHeight = Math.max.apply(null, displayList.map(getHeight));
var row:Number;
var col:Number;
for (i = 0; i < displayList.length; i++)
{
if (direction == 0)
{
// horizon
row = Math.floor(i / turnCount);
col = i % turnCount;
}
else if (direction == 1)
{
// vertical
row = i % turnCount;
col = Math.floor(i / turnCount);
}
displayList[i].x = horizontalMargin + col * horizontalMargin + col * maxWidth;
displayList[i].y = verticalMargin + row * verticalMargin + row * maxHeight;
if (centering)
{
displayList[i].x += (maxWidth - displayList[i].width) / 2;
displayList[i].y += (maxHeight - displayList[i].height) / 2;
}
}
}
import flash.display.DisplayObject;
/**
* get width
* @param element
* @param index
* @param arr
* @return
*/
function getWidth(element:*, index:int, arr:Array):Number {
return element.width;
}
/**
* get height
* @param element
* @param index
* @param arr
* @return
*/
function getHeight(element:*, index:int, arr:Array):Number {
return element.height;
}