random avatars
/**
* Copyright shaktool ( http://wonderfl.net/user/shaktool )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/s8x6
*/
package {
import flash.display.*;
import flash.events.*;
public class FlashTest extends Sprite {
public function FlashTest() {
stage.addEventListener(MouseEvent.CLICK, onClick);
onClick(null);
}
private function makeColor(hue: Number, scale: Number, offset: Number, flip: Boolean): Object {
hue -= int(hue/3.0) * 3.0;
var redRatio: Number = hue < 1.0 ? (1.0 - hue) : (hue > 2.0 ? hue - 2.0 : 0.0);
var greenRatio: Number = hue > 0.0 && hue < 2.0 ? 1.0 - Math.abs(hue - 1.0) : 0.0;
var blueRatio: Number = hue > 1.0 && hue < 3.0 ? 1.0 - Math.abs(hue - 2.0) : 0.0;
redRatio *= 0.85;
greenRatio *= 0.7;
if (flip) {
redRatio = 1.0 - redRatio;
greenRatio = 1.0 - greenRatio;
blueRatio = 1.0 - blueRatio;
}
redRatio = redRatio * scale + offset;
greenRatio = greenRatio * scale + offset;
blueRatio = blueRatio * scale + offset;
return {r: redRatio, g: greenRatio, b: blueRatio};
}
private function blendColors(first: Object, second: Object, ratio: Number): Object {
return {r: first.r * (1 - ratio) + second.r * ratio,
g: first.g * (1 - ratio) + second.g * ratio,
b: first.b * (1 - ratio) + second.b * ratio};
}
private function convertColor(color: Object): int {
return (color.r * 255 << 16) + (color.g * 255 << 8) + (color.b * 255 << 0);
}
private function onClick(event: MouseEvent): void {
var i: int;
var j: int;
graphics.clear();
graphics.beginFill(0);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
while(numChildren > 0) removeChildAt(0);
var data: BitmapData = new BitmapData(16, 16, false, 0xff0000);
var bitmap: Bitmap = new Bitmap(data);
addChild(bitmap);
bitmap.scaleX = bitmap.scaleY = 4;
bitmap.x = (464 - bitmap.width) / 2;
bitmap.y = (464 - bitmap.height) / 2;
var background: Boolean = Math.random() > 0.5;
var dark1: Object = makeColor(3.0 * Math.random(), 0.9, background ? 0.0 : 0.0, background);
var dark2: Object = makeColor(3.0 * Math.random(), 0.9, background ? 0.0 : 0.0, background);
var dark3: Object = makeColor(3.0 * Math.random(), 0.3, background ? 0.7 : 0.0, background);
var light1: Object = makeColor(3.0 * Math.random(), 0.9, background ? 0.0 : 0.0, !background);
var light2: Object = makeColor(3.0 * Math.random(), 0.9, background ? 0.0 : 0.0, !background);
var light3: Object = makeColor(3.0 * Math.random(), 0.3, background ? 0.0 : 0.7, !background);
var pattern: Array = [];
pattern.length = 9 * 9;
for (i = 0; i < 9; i++) {
pattern[i] = false;
}
for (j = 1; j < 8; j++) {
pattern[j * 9] = false;
for (i = 1; i < 5; i++) {
pattern[j * 9 + i] = pattern[j * 9 + 8 - i] = Math.random() > 0.5;
}
pattern[j * 9 + 8] = false;
}
for (i = 0; i < 9; i++) {
pattern[8 * 9 + i] = false;
}
var noise: Array = [];
for (j = 0; j < 8; j++) {
for (i = 0; i < 8; i++) {
noise[j * 8 + i] = Math.random() > 0.5;
}
}
for (j = 0; j < 16; j++) {
for (i = 0; i < 16; i++) {
var patternOn: Boolean = pattern[((j+1)>>1) * 9 + ((i+1)>>1)];
var noiseOn: Boolean = noise[(j>>1) * 8 + (i>>1)];
var color: int = patternOn ? convertColor(noiseOn ? light3 : (i < 8 ? light1 : light2)) : convertColor(noiseOn ? (j < 8 ?dark1 : dark2) : dark3);
//var color: int = patternOn ? convertColor(noiseOn ? light3 : blendColors(light1, light2, i / 15)) : convertColor(noiseOn ? blendColors(dark1, dark2, j / 15) : dark3);
data.setPixel(i, j, color);
}
}
}
}
}