Kulers
/**
* Copyright bongiovi015 ( http://wonderfl.net/user/bongiovi015 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ikfV
*/
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Shader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.filters.DropShadowFilter;
import flash.filters.ShaderFilter;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.ByteArray;
import frocessing.color.ColorHSL;
import net.hires.debug.Stats;
[SWF(width=465, height=465, frameRate=30, backgroundColor=0)]
public class ColorReplace extends Sprite {
//--------------------------------------
// CLASS CONSTANTS
//--------------------------------------
private var loaderShader : URLLoader = new URLLoader;
private var tfDebug:TextField;
private var format:TextFormat;
private var bmpd:BitmapData;
private var shader:Shader;
private var seed:int = Math.random() * 0xFFFF;
private var offset:Array = [new Point, new Point];
private var SPEED:Number = 2;
private var bmpdTemplate:BitmapData;
private var scaleFactor:Number = 2;
public function ColorReplace() {
bmpd = new BitmapData(465/scaleFactor, 465/scaleFactor, false, 0);
var bmp:Bitmap = Bitmap(addChild(new Bitmap(bmpd)));
bmp.scaleX = bmp.scaleY = scaleFactor;
bmpdTemplate = new BitmapData(100, 20, false, 0xFFFFFF);
var bmpTemp:Bitmap = new Bitmap(bmpdTemplate);
addChild(bmpTemp);
bmpTemp.x = 5;
bmpTemp.y = 25;
bmpTemp.filters = [new DropShadowFilter(2, 45, 0, .5, 3, 3, 1, 3)];
format = new TextFormat("Arial", 12, 0xFFFFFF);
tfDebug = TextField(addChild(new TextField));
tfDebug.defaultTextFormat = format;
tfDebug.x = 5;
tfDebug.y = 5;
tfDebug.autoSize = "left";
tfDebug.filters = [new DropShadowFilter(2, 45, 0, .5, 3, 3, 1, 3)];
__loadShader();
addChild(new Stats).y = 365;
}
private function __loadShader() : void {
tfDebug.text = "Loading Shader ... ";
loaderShader.addEventListener(Event.COMPLETE, shaderLoaded);
loaderShader.dataFormat = URLLoaderDataFormat.BINARY;
loaderShader.load(new URLRequest("http://www.bongiovi.tw/wonderfl/ColorReplace.pbj"));
}
private function shaderLoaded(e:Event) : void {
shader = new Shader();
shader.byteCode = ByteArray(loaderShader.data);
getColor();
stage.addEventListener(MouseEvent.CLICK, getColor);
}
private function getColor(e:Event=null) : void {
tfDebug.text = "Getting Color From Adobe Kuler ... ";
removeEventListener(Event.ENTER_FRAME, __loop);
KulerManager.getRandomColor(__onColors);
}
private function __onColors(colors:Array) : void {
tfDebug.text = "Click to change the color";
colors = shuffle(colors);
var c:ColorHSL = new ColorHSL;
shader.data.srcPerlin.input = bmpd;
c.value = colors[0];
shader.data.color0.value = [c.r/255, c.g/255, c.b/255, 1];
bmpdTemplate.fillRect(new Rectangle(0, 0, 20, 20), c.value);
c.value = colors[1];
shader.data.color1.value = [c.r/255, c.g/255, c.b/255, 1];
bmpdTemplate.fillRect(new Rectangle(20, 0, 20, 20), c.value);
c.value = colors[2];
shader.data.color2.value = [c.r/255, c.g/255, c.b/255, 1];
bmpdTemplate.fillRect(new Rectangle(40, 0, 20, 20), c.value);
c.value = colors[3];
shader.data.color3.value = [c.r/255, c.g/255, c.b/255, 1];
bmpdTemplate.fillRect(new Rectangle(60, 0, 20, 20), c.value);
c.value = colors[4];
shader.data.color4.value = [c.r/255, c.g/255, c.b/255, 1];
bmpdTemplate.fillRect(new Rectangle(80, 0, 20, 20), c.value);
shader.data.offset.value = [.3];
shader.data.gap.value = [.1];
addEventListener(Event.ENTER_FRAME, __loop);
}
private function __loop(e:Event) : void {
bmpd.perlinNoise(465/scaleFactor, 465/scaleFactor, 3, seed, true, true, 7, true, offset);
bmpd.applyFilter(bmpd, bmpd.rect, new Point, new ShaderFilter(shader));
bmpd.applyFilter(bmpd, bmpd.rect, new Point, new BlurFilter(3, 3, 3));
offset[0].x += SPEED;
offset[1].y += SPEED;
}
public static function shuffle(ary:Array) : Array {
var resultArray:Array = ary.concat();
for (var i:int = 0; i < resultArray.length; i++){
var tmp:* = resultArray[i];
var randomNum:Number = Math.floor(Math.random() * resultArray.length);
resultArray[i] = resultArray[randomNum];
resultArray[randomNum] = tmp;
}
return resultArray;
}
}
}
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLRequest;
class KulerManager extends EventDispatcher{
//--------------------------------------
// CLASS CONSTANTS
//--------------------------------------
public static const NAMESPACE_KULER : String = "http://kuler.adobe.com/kuler/API/rss/";
private static const APIKEY : String = "4A297340291400B78BF1DFFE0E8E1678";
private static const kuler : Namespace = new Namespace(NAMESPACE_KULER);
private static var _callback : Function;
public static var xmlColors : XML;
public function KulerManager() : void { }
public static function getRandomColor(callBack:Function) : void {
_callback = callBack;
if(xmlColors == null) {
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, __onColorHandler);
loader.load(new URLRequest("http://kuler-api.adobe.com/rss/get.cfm?listType=rating&startIndex=0&itemsPerPage=100&key=" + APIKEY));
} else {
getRandomColorFromXML();
}
}
private static function getRandomColorFromXML() : void {
var list:XMLList = new XMLList(xmlColors.channel.item);
var index : int = int(Math.random() * list.length());
var theme:XML = list[index];
var swatchList:XMLList = new XMLList(theme.kuler::themeItem.kuler::themeSwatches.kuler::swatch);
var i : int = 0;
var colors:Array = [];
var sColor:String;
var color:uint
for(i=0; i<swatchList.length(); i++) {
sColor = swatchList[i].kuler::swatchHexColor;
color = parseInt("0x"+sColor);
colors.push(color);
}
if(_callback != null) _callback(colors);
}
private static function __onColorHandler(e:Event):void {
xmlColors = XML(e.currentTarget.data);
getRandomColorFromXML();
}
}
/*
<languageVersion : 1.0;>
kernel AdjustVelocity
< namespace : "bongiovi.tw";
vendor : "bongiovi";
version : 1;
description : "Black Hole Effect";
>
{
input image4 src;
input image4 srcPerlin;
output pixel4 dst;
parameter float gap
<
minValue:0.0;
maxValue:1.0;
defaultValue:0.2;
>;
parameter float offset
<
minValue:0.0;
maxValue:1.0;
defaultValue:0.0;
>;
parameter float4 color0
<
minValue:float4(0.0, 0.0, 0.0, 0.0);
maxValue:float4(1.0, 1.0, 1.0, 1.0);
defaultValue:float4(1.0, 0.0, 0.0, 1.0);
>;
parameter float4 color1
<
minValue:float4(0.0, 0.0, 0.0, 0.0);
maxValue:float4(1.0, 1.0, 1.0, 1.0);
defaultValue:float4(0.0, 1.0, 0.0, 1.0);
>;
parameter float4 color2
<
minValue:float4(0.0, 0.0, 0.0, 0.0);
maxValue:float4(1.0, 1.0, 1.0, 1.0);
defaultValue:float4(0.0, 0.0, 1.0, 1.0);
>;
parameter float4 color3
<
minValue:float4(0.0, 0.0, 0.0, 0.0);
maxValue:float4(1.0, 1.0, 1.0, 1.0);
defaultValue:float4(1.0, 0.0, 1.0, 1.0);
>;
parameter float4 color4
<
minValue:float4(0.0, 0.0, 0.0, 0.0);
maxValue:float4(1.0, 1.0, 1.0, 1.0);
defaultValue:float4(0.0, 1.0, 1.0, 1.0);
>;
void
evaluatePixel()
{
float2 pos = outCoord();
float4 perlin = sampleLinear(src, pos);
perlin = sampleLinear(srcPerlin, pos);
if(perlin.r < gap + offset) {
dst = mix(color0, color1, (perlin.r - offset) / gap);
} else if (perlin.r < gap*2.0 + offset) {
dst = mix(color1, color2, (perlin.r - gap - offset) / gap);
} else if (perlin.r < gap*3.0 + offset) {
dst = mix(color2, color3, (perlin.r - gap*2.0 - offset) / gap);
} else {
dst = mix(color3, color4, (perlin.r - gap*3.0 - offset) / gap);
}
}
}
*/