topical media & game development

talk show tell print

graphic-canvas-util-pixastic-actions-desaturate.js / js



  /*
   * Pixastic Lib - Desaturation filter - v0.1.1
   * Copyright (c) 2008 Jacob Seidelin, jseidelin@nihilogic.dk, http://blog.nihilogic.dk/
   * MIT License [http://www.opensource.org/licenses/mit-license.php]
   */
  
  Pixastic.Actions.desaturate = {
  
          process : function(params) {
                  var useAverage = !!params.options.average;
  
                  if (Pixastic.Client.hasCanvasImageData()) {
                          var data = Pixastic.prepareData(params);
                          var rect = params.options.rect;
                          var w = rect.width;
                          var h = rect.height;
  
                          var p = w*h;
                          var pix = p*4, pix1, pix2;
  
                          if (useAverage) {
                                  while (p--) 
                                          data[pix-=4] = data[pix1=pix+1] = data[pix2=pix+2] = (data[pix]+data[pix1]+data[pix2])/3
                          } else {
                                  while (p--)
                                          data[pix-=4] = data[pix1=pix+1] = data[pix2=pix+2] = (data[pix]*0.3 + data[pix1]*0.59 + data[pix2]*0.11);
                          }
                          return true;
                  } else if (Pixastic.Client.isIE()) {
                          params.image.style.filter += " gray";
                          return true;
                  }
          },
          checkSupport : function() {
                  return (Pixastic.Client.hasCanvasImageData() || Pixastic.Client.isIE());
          }
  }


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.