CMY color
...
@author @kndys
package
{
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.GradientType;
import flash.display.Graphics;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.media.Camera;
import flash.media.Video;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
/**
* ...
* @author @kndys
*/
[SWF(width="465",height="465",backgroundColor="0xffffff",frameRate="30")]
public class Main extends Sprite
{
private const N:uint = 20;
private const W:uint = 465;
private const H:uint = 465;
private var unitY:Number = Number(H) / N;
private var unitX:Number = unitY * Math.sqrt(3);
private const M:uint = Math.ceil(W / unitX);
private var r:Number = 0.5 * unitY;
private var origin1:Pt = new Pt(r, r);
private var origin2:Pt = new Pt(r + 0.5 * unitX, 0)
private var s2r:Vector.<Number> = new Vector.<Number>();
private var circle0:Circle;
private var video:Video;
private var camera:Camera;
private var bmd:BitmapData;
private var chR:Sprite;
private var chG:Sprite;
private var chB:Sprite;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var i:uint, j:uint;
for (i = 0; i < 256; i++)
{
s2r[i] = r * Math.sqrt(Number(i) / 256.0);
}
var c:Circle = null;
var x01:Number, y01:Number, x02:Number, y02:Number;
x01 = origin1.x;
y01 = origin1.y;
x02 = origin2.x;
y02 = origin2.y;
for (i = 0; i < M; i += 1)
{
for (j = 0; j< N; j+=1)
{
c = new Circle(x01 + i * unitX, y01 + j * unitY, r, c);
c = new Circle(x02 + i * unitX, y02 + j * unitY, r, c);
}
c = new Circle(x02 + i * unitX, y02 + N * unitY, r, c);
}
circle0 = c;
camera = Camera.getCamera();
if (camera)
{
camera.setMode(W, H, 30);
video = new Video(W, H);
video.attachCamera(camera);
}
else
{
var url:String = "http://assets.wonderfl.net/images/related_images/b/b3/b3b8/b3b88b2720bf6e8ee36d391110f116d0bc0a5b2d";
var loader:Loader = new Loader();
loader.load(new URLRequest(url), new LoaderContext(true));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(evt:Event):void {
evt.target.removeEventListener(Event.COMPLETE, arguments.callee);
bmd.draw(loader, new Matrix(W/loader.width, 0, 0, H/loader.height));
});
}
bmd = new BitmapData(W, H);
chR = new Sprite();
chG = new Sprite();
chB = new Sprite();
blendMode = BlendMode.LAYER;
chR.blendMode = BlendMode.SUBTRACT;
chG.blendMode = BlendMode.SUBTRACT;
chB.blendMode = BlendMode.SUBTRACT;
addChild(chR);
addChild(chG);
addChild(chB);
graphics.beginFill(0xffffff);
graphics.drawRect(0, 0, W, H);
graphics.endFill();
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void
{
if (video)
{
bmd.draw(video);
}
else
{
}
var gR:Graphics = chR.graphics;
gR.clear();
gR.beginFill(0xff0000);
var gG:Graphics = chG.graphics;
gG.clear();
gG.beginFill(0xff00);
var gB:Graphics = chB.graphics;
gB.clear();
gB.beginFill(0xff);
var c:Circle = circle0;
var rgb:uint;
var cR:uint, cG:uint, cB:uint;
var rect:Rectangle = new Rectangle(0,0,2*r, 2*r);
do {
rect.x = c.x;
rect.y = c.y;
rgb = averageColor(bmd, rect);
cR = (~rgb >> 16) & 0xff;
cG = (~rgb >> 8) & 0xff;
cB = ~rgb & 0xff;
gR.drawCircle(c.x-2, c.y-2, s2r[cR]);
gG.drawCircle(c.x + 5, c.y, s2r[cG]);
gB.drawCircle(c.x, c.y + 5, s2r[cB]);
}while (c = c.next);
}
private function averageColor(data:BitmapData, rect:Rectangle):uint
{
var bytes:ByteArray = data.getPixels(rect);
bytes.position = 0;
var r:uint = 0, g:uint = 0, b:uint = 0;
var len:uint = bytes.length >> 5;
for (var i:int = 0; i< len; i+=1)
{
bytes.readUnsignedInt();
bytes.readUnsignedInt();
bytes.readUnsignedInt();
bytes.readUnsignedInt();
bytes.readUnsignedInt();
bytes.readUnsignedInt();
bytes.readUnsignedInt();
bytes.readUnsignedByte();
r += bytes.readUnsignedByte();
g += bytes.readUnsignedByte();
b += bytes.readUnsignedByte();
}
r /= len;
g /= len;
b /= len;
return r << 16 | g << 8 | b;
}
}
}
class Pt
{
public var x:Number;
public var y:Number;
public function Pt(x:Number = 0, y:Number = 0)
{
this.x = x;
this.y = y;
}
}
class Circle extends Pt
{
public var r:Number;
public var next:Circle;
public function Circle(x:Number = 0, y:Number = 0, r:Number = 0, next:Circle = null)
{
super(x, y);
this.r = r;
this.next = next;
}
}