Return to Snippet

Revision: 40100
at January 26, 2011 02:58 by adrianparr


Initial Code
package 
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.ColorTransform;
	
	import sekati.utils.ColorUtil;

	public class Main extends Sprite 
	{
		
		[Embed(source="../assets/hero.jpg")]
		private var StillImage:Class;
		
		
		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);
			
			var image:Bitmap = new StillImage();
			addChild(image);
			
			var swatch:Sprite = new Sprite();
			swatch.graphics.beginFill(0xFF0000);
			swatch.graphics.drawRect(0, 0, image.width, 40);
			swatch.graphics.endFill();
			swatch.x = image.x;
			swatch.y = image.y + image.height;
			addChild(swatch);
			
			var averageRed:int = ColorUtil.averageRed(image);
			var averageGreen:int = ColorUtil.averageGreen(image);
			var averageBlue:int = ColorUtil.averageBlue(image);
			
			var colour:uint = averageRed << 16 | averageGreen << 8 | averageBlue;
			
			trace(averageRed + ", " + averageGreen + ", " + averageBlue);
			
			var ct:ColorTransform = new ColorTransform();
			ct.color = (colour);
			swatch.transform.colorTransform = ct;

			var hexColour:String = "#" + colour.toString(16).toUpperCase();
			trace("hexColour: "+hexColour);
			
		}
		
	}
	
}

Initial URL
http://code.google.com/p/sekati/

Initial Description
The docs for the Sekati SWC are here: http://docs.sekati.com/sekati/sekati/utils/ColorUtil.html   

Another useful blog post: http://blog.soulwire.co.uk/code/actionscript-3/extract-average-colours-from-bitmapdata

Initial Title
AS3 Get Average Colour of an Image

Initial Tags
image, color

Initial Language
ActionScript 3