topical media & game development

talk show tell print

graphic-flex-image-effects-05-source-desaturate.pbk / pbk



  <languageVersion: 1.0;>
  
  kernel Desaturate
  <
      namespace:      "com.27Bobs";
      vendor:         "Todd Yard";
      version:        1;
      description:    "Desaturates an image by a specified amount.";
  >
  {
      parameter float percent
      <
          minValue:       0.0;
          maxValue:       1.0;
          defaultValue:   1.0;
          description:    "Percent image should be desaturated.";
      >;
  
      const float3 grayValues = float3(0.3, 0.59, 0.11);
      
      input image4 source;
      output pixel4 result;
      
      void
      evaluatePixel()
      {
          float4 px = sampleNearest(source, outCoord());
          float distanceToDesaturatedValues = dot(px.rgb, grayValues);
          float3 fullDesaturate = float3(distanceToDesaturatedValues);
          float3 noDesaturate = px.rgb;
          px.rgb = mix(noDesaturate, fullDesaturate, percent);
          result = px;
      }
      
  }


(C) Æliens 18/6/2009

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.