/**
* Copyright shapevent ( http://wonderfl.net/user/shapevent )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/AfmO
*/
package {
// http://actionsnippet.com/?p=1627
import flash.display.*;
import flash.events.*;
[SWF(width=560,height=300,backgroundColor=0x000000,frameRate=30)]
dynamic public class Test extends MovieClip {
private var key:Object;
private var alphabet:Array;
private var num:Number;
private var step:Number;
private var colors:Object;
private var count:int;
private var size:int;
public function Test(){
// init
key = new Object();
alphabet = "abcdefghijklmnopqrstuvwxyz".split("");
num = alphabet.length;
step = 360 / num;
colors = new Object();
for (var i:int = 0; i<num; i++){
var index:String = alphabet[i];
key[index] = 65 + i;
var c:Array = hsv(i * step, 1, 1);
colors[index] = c[0] <<16 | c[1] <<8 | c[2];
}
alphabet.push("32");
num++;
key["32"] = 32;
colors["32"] = 0x333333;
x = y = 10;
count = 0;
size = 20;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
// ported from here:
//http://www.cs.rit.edu/~ncs/color/t_convert.html
}
// private methods
private function onKeyPressed(evt:KeyboardEvent):void{
for (var i:int= 0; i<num; i++){
var index:String = alphabet[i];
if (index == "32"){
trace("hi", evt.keyCode, key[index]);
}
if (evt.keyCode == key[index]){
graphics.beginFill(colors[index]);
var xp:int = count % num * size;
var yp:int = int(count / num) * size;
graphics.drawRect(xp, yp, size, size);
count++;
}
}
}
private function hsv(h:Number, s:Number, v:Number):Array{
var r:Number, g:Number, b:Number;
var i:int;
var f:Number, p:Number, q:Number, t:Number;
if (s == 0){
r = g = b = v;
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
h /= 60;
i = Math.floor(h);
f = h - i;
p = v * (1 - s);
q = v * (1 - s * f);
t = v * (1 - s * (1 - f));
switch( i ) {
case 0:
r = v, g = t, b = p;
break;
case 1:
r = q, g = v, b = p;
break;
case 2:
r = p, g = v, b = t;
break;
case 3:
r = p, g = q, b = v;
break;
case 4:
r = t, g = p, b = v;
break;
default: // case 5:
r = v, g = p, b = q;
break;
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
}
}