Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Grey scale images with Actionscript 3.0

This is a discussion on Grey scale images with Actionscript 3.0 within the Tutorials forums, part of the Flash English category; In this article, we will see how to apply grey scale effect to an image. This effect is possible converting ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  4 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 27-09-07, 21:04
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Grey scale images with Actionscript 3.0

In this article, we will see how to apply grey scale effect to an image.
This effect is possible converting all the colors of the image to an equal luminosity.
This can be done multiplying the colors by the constant luminosity of the red, green and blue channels (RGB scale).
With Actionscript 3.0, we need a matrix and the ColoroMatrixFilter Class.

Let's see how to do it...

I create a FLA and save it as 'scala_di_grigi.fla', in which I import an image (colored one) and convert it as a MovieClip with an instance name 'image_mc'
I create a Document Class, an AS file saved as 'ScalaGrigi.as', implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.BitmapFilter;
	import flash.geom.Point;
	
	public class ScalaGrigi extends MovieClip
	{
		private var bit_data:BitmapData;
		
		private var clip_mc:MovieClip;
		
		public function ScalaGrigi()
		{
			init();
		}
		
		private function init():void
		{
			clip_mc=new MovieClip();
			bit_data=new BitmapData(image_mc.width,image_mc.height,true,0xFF000000);
			bit_data.draw(image_mc);
			bit_data.applyFilter(bit_data,bit_data.rect,new Point(0,0),convertiGrigio());
			var bitmap:Bitmap=new Bitmap(bit_data);
			clip_mc.addChild(bitmap);
			addChild(clip_mc);
			clip_mc.x=image_mc.x;
			clip_mc.y=image_mc.y+image_mc.height+10;
		}
		
		private function convertiGrigio():BitmapFilter
		{
			var r:Number=0.212671;
			var g:Number=0.715160;
			var b:Number=0.072169;
			return new ColorMatrixFilter
			(
				[r,g,b,0,0,
				r,g,b,0,0,
				r,g,b,0,0,
				0,0,0,1,0]   );
		}
	}
}
The result:







Let's analise the code:

Properties

a BitmapData instance which will take a picture of image_mc
private var bit_data:BitmapData;
a MovieClip instance which will contain the Bitmap
private var clip_mc:MovieClip;

Methods
init();
I create a MovieClip
clip_mc=new MovieClip();
I create a BitmapData with a width and heigth equal to the measure of image_mc
bit_data=new BitmapData(image_mc.width,image_mc.height,true,0xF F000000);
I take a picture of image_mc
bit_data.draw(image_mc);
I apply a filter to the BitmapData passing the values of: the same BitmapData, the rect property of the bitmapData (which is nothing else then an instance of the Rectangle Class), the coordinates of the filter's starting point, a bitmapFilter instance which will return the function convertiGrigio()
bit_data.applyFilter(bit_data,bit_data.rect,new Point(0,0),convertiGrigio());
I create a new Bitmap (local variable because I do not need to keep track of it) to which I pass the BitmapData as value
var bitmap:Bitmap=new Bitmap(bit_data);
I add the Bitmap to clip_mc
clip_mc.addChild(bitmap);
I add clip_mc to the DisplayObject (otherwise it would not be visible)
addChild(clip_mc);
I position clip_mc
clip_mc.x=image_mc.x;
clip_mc.y=image_mc.y+image_mc.height+10;

convertiGrigio();
I create three numerical variables (constant ones) with the respective values of the luminosity of the three red, green, blue channels (RGB scale)
const r:Number=0.212671;
const g:Number=0.715160;
const ar b:Number=0.072169;
I return a new instance of the ColorMatrixFilter Class which is a matrix containing the value for a grey scale
return new ColorMatrixFilter
(
[r,g,b,0,0,
r,g,b,0,0,
r,g,b,0,0,
0,0,0,1,0] );

Stay tuned !
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
FullScreen images gallery Flep FlepStudio utilities 6 19-12-08 21:20
Illusion - images gallery Flep FlepStudio utilities 12 16-12-08 01:52
Images Scroller Flep FlepStudio utilities 45 05-12-08 16:27
unload images karonte Actionscript 3.0 base 2 14-05-08 20:01
parent scale vs child scale dedavai advanced Actionscript 3.0 1 08-12-07 06:53


All times are GMT. The time now is 16:26.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap