Fast brightness, contrast, saturation



20 Comments

Share




Sorry for the late update. I had lots of work & devs recently and there’re still more to go.
But, still i’ve found some spare time to post this little (hope useful) utility class.


////////////////////////////////////////////////////////////////////////////////
//
//  Copyright 2010 Julius Loa | jloa@chargedweb.com
//  All Rights Reserved.
//  license: GNU {http://www.opensource.org/licenses/gpl-2.0.php}
//  notice: just keep the header plz
//
////////////////////////////////////////////////////////////////////////////////

package com.chargedweb.utils
{
	import flash.filters.ColorMatrixFilter;
	
	/**
	 * Matrix utility class
	 * @version 1.0
	 * so far added:
	 * - brightness
	 * - contrast
	 * - saturation
	 */
	public class MatrixUtil
	{
		/**
		 * sets brightness value available are -100 ~ 100 @default is 0
		 * @param 		value:int	brightness value
		 * @return		ColorMatrixFilter
		 */
		public static function setBrightness(value:Number):ColorMatrixFilter
		{
			value = value*(255/250);
			
			var m:Array = new Array();
    		m = m.concat([1, 0, 0, 0, value]);	// red
   		 	m = m.concat([0, 1, 0, 0, value]);	// green
   		 	m = m.concat([0, 0, 1, 0, value]);	// blue
    		m = m.concat([0, 0, 0, 1, 0]);		// alpha
    		
    		return new ColorMatrixFilter(m);
		}
		
		/**
		 * sets contrast value available are -100 ~ 100 @default is 0
		 * @param 		value:int	contrast value
		 * @return		ColorMatrixFilter
		 */
		public static function setContrast(value:Number):ColorMatrixFilter
		{
			value /= 100;
			var s: Number = value + 1;
    		var o : Number = 128 * (1 - s);
			
			var m:Array = new Array();
			m = m.concat([s, 0, 0, 0, o]);	// red
			m = m.concat([0, s, 0, 0, o]);	// green
			m = m.concat([0, 0, s, 0, o]);	// blue
			m = m.concat([0, 0, 0, 1, 0]);	// alpha
			
			return new ColorMatrixFilter(m);
		}
		
		/**
		 * sets saturation value available are -100 ~ 100 @default is 0
		 * @param 		value:int	saturation value
		 * @return		ColorMatrixFilter
		 */
		public static function setSaturation(value:Number):ColorMatrixFilter
		{
			const lumaR:Number = 0.212671;
    		const lumaG:Number = 0.71516;
    		const lumaB:Number = 0.072169;
			
			var v:Number = (value/100) + 1;
			var i:Number = (1 - v);
   		 	var r:Number = (i * lumaR);
    		var g:Number = (i * lumaG);
    		var b:Number = (i * lumaB);
			
			var m:Array = new Array();
			m = m.concat([(r + v), g, b, 0, 0]);	// red
			m = m.concat([r, (g + v), b, 0, 0]);	// green
			m = m.concat([r, g, (b + v), 0, 0]);	// blue
			m = m.concat([0, 0, 0, 1, 0]);			// alpha
			
 			return new ColorMatrixFilter(m);
		}
	}
}

And the sample usage code:


import com.chargedweb.utils.MatrixUtil;

var bmp:Bitmap = new Bitmap(new Image(0,0));
addChild(bmp);
bmp.filters = [MatrixUtil.setBrightness(20),
			   MatrixUtil.setContrast(20),
			   MatrixUtil.setSaturation(-100)];

20 Comments (+add yours?)

  1. biril
    Apr 30, 2010 @ 03:35:11

    Line 31: value = value*(255/255); ?

  2. Ramon Fritsch
    Apr 30, 2010 @ 08:29:47

    Nice job. You could improve your code on line 31: “value = value*(255/255);”
    (255/255) is equal to 1 so. You might remove that line. :) cheers

  3. jloa
    Apr 30, 2010 @ 17:08:39

    That’s soooooooooo LOL :)
    Thx guys. Damn, u shamed me i’ve must’ve mistaken and haven’t noticed that.

  4. Alvina
    Jun 24, 2010 @ 23:42:51

    was sailing by internet and found your blog. Reciprocate the visit http://sensuaisegatas.blogspot.com

  5. free registry cleaner
    Aug 05, 2010 @ 15:41:19

    Hey I think you have a great blog going here, I found it on Bing and plan on returning regularly for the information that you all are providing.

  6. The Guru Code
    Nov 10, 2010 @ 20:41:32

    Your site looks funny in google chrome

  7. Jerica Gerpheide
    Nov 12, 2010 @ 05:18:21

    Thanks for the post. I thought it was good.

  8. Eldora Bonker
    Dec 13, 2010 @ 07:13:39

    Most of the times i visit a blog I notice that most blogs are amateurish. Regarding your blog,I have to say that you have done a good job here.

  9. Bronwyn Loreg
    Dec 13, 2010 @ 07:35:32

    its good as your other content : D, appreciate it for posting .

  10. Mirtha Roberti
    Dec 22, 2010 @ 22:56:21

    I’d constantly want to be update on new articles on this website , saved to bookmarks ! .

  11. Ritesh
    Jul 07, 2011 @ 16:07:26

    Its simple and very useful. Thank you for your post, you saved my day :)

  12. wsvdmeer
    Apr 19, 2012 @ 01:17:44

  13. raf
    Apr 24, 2012 @ 09:45:11

    Thanks a lot!

  14. Joe Bradfield
    Dec 17, 2012 @ 16:03:42

    would this work in AS2.0
    where would i start i am new to using classes and the sort
    thanks in advance for any help

  15. jloa
    Dec 24, 2012 @ 00:55:01

    Nope, as3 only.
    I would recommend you reading this book http://shop.oreilly.com/product/9780596526948.do

  16. shubniggurath
    Jan 24, 2013 @ 11:14:25

    Thank you very much! Really fast and simple.

  17. Dave Stewart
    Feb 25, 2013 @ 17:31:14

    Can the matrices be combined into one matrix by simply updating the values from the last operation?

  18. jloa
    Mar 02, 2013 @ 01:54:28

    Yes

  19. jloa
    Mar 02, 2013 @ 01:54:39

    u r welcome

  20. GGPlay
    Jun 07, 2013 @ 11:01:20

    Hey man, so awesome, a much time saver for all of us !!!!!

Leave a Reply

Spam protection by WP Captcha-Free