Posted By

ricosushi on 02/02/11


Tagged

color saturation desaturate


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

ricosushi
erengoc


image saturation


 / Published in: ActionScript
 

Desaturates/Saturates a BMP

  1. // saturation libraries
  2. import flash.filters.BitmapFilter;
  3. import flash.filters.ColorMatrixFilter;
  4.  
  5. // Turn an image (gradually) into a grayscale
  6. // t is a Number ranging from 0 == 100% saturation, full colors
  7. // to 1 == fully grayscale (is default)
  8. function getDesaturationFilter(t:Number):BitmapFilter{
  9. t = t ? t : 1;
  10. // luminance coefficients as by Charles A. Poynton, 1997
  11. // see point C-9 of http://www.faqs.org/faqs/graphics/colorspace-faq/
  12. // alternative coefficients by Paul Haeberly :http://www.sgi.com/misc/grafica/matrix/
  13. var r = 0.212671;
  14. var g = 0.715160;
  15. var b = 0.072169;
  16. return new ColorMatrixFilter(
  17. [t*r+1-t, t*g, t*b, 0, 0,
  18. t*r, t*g+1-t, t*b, 0, 0,
  19. t*r, t*g, t*b+1-t, 0, 0,
  20. 0, 0, 0, 1, 0]);
  21. }
  22.  
  23.  
  24. // saturation velocity
  25. var satInc:Number = 0.08;
  26.  
  27.  
  28. // saturates image
  29. function saturate(mc:MovieClip):Void{
  30.  
  31. // 1 = dsaturate
  32. // 0.01 saturate
  33. var sat = 1;
  34.  
  35. mc.onEnterFrame = function(){
  36.  
  37. sat -= satInc;
  38.  
  39. if(sat < 0.01){
  40. sat = 0.01;
  41.  
  42. // remove control
  43. delete mc.onEnterFrame;
  44. }
  45.  
  46. mc.filters = new Array(getDesaturationFilter(sat));
  47. }// enterframe
  48.  
  49. }
  50.  
  51.  
  52.  
  53. // desaturates image
  54. function desaturate(mc:MovieClip):Void{
  55.  
  56. // 1 = dsaturate
  57. // 0.1 saturate
  58. var sat = 0.1;
  59.  
  60. mc.onEnterFrame = function(){
  61.  
  62. sat += satInc;
  63.  
  64. if(sat > 1){
  65. sat = 1;
  66.  
  67. // remove control
  68. delete mc.onEnterFrame;
  69. }
  70.  
  71. mc.filters = new Array(getDesaturationFilter(sat));
  72. }// enterframe
  73.  
  74. }
  75.  
  76.  
  77. // Usage
  78. function thumbOut():Void {
  79. desaturate(this.loader_MC);
  80. }

Report this snippet  

You need to login to post a comment.