Guest
Public paste!

Pixel Bender Color Blend mode

By: a guest | Jun 24th, 2010 | Syntax: None | Size: 1.36 KB | Hits: 298 | Expires: Never
Copy text to clipboard
  1. <languageVersion : 1.0;>
  2.  
  3.  
  4. kernel ColorBlendMode
  5. <   namespace : "org.sroucheray";
  6.     vendor : "Stéphane Roucheray";
  7.     version : 1;
  8.     description : "Photoshop Color BlendMode"; >
  9. {
  10.     parameter float percent
  11.     <
  12.         minValue:       0.0;
  13.         maxValue:       1.0;
  14.         defaultValue:   1.0;
  15.     >;
  16.    
  17.     input image4 frontImage;
  18.     input image4 backImage;
  19.     output pixel4 dst;
  20.    
  21.     void evaluatePixel()
  22.     {
  23.         pixel4 cs = sampleNearest(frontImage, outCoord());
  24.         pixel4 cb = sampleNearest(backImage, outCoord());
  25.         pixel4 fullBlend;
  26.        
  27.         //Lum(Cb)
  28.         float lumCb = 0.3 * cb.r + 0.59 * cb.g + 0.11 * cb.b;
  29.         //Lum(Cs)
  30.         float lumCs = 0.3 * cs.r + 0.59 * cs.g + 0.11 * cs.b;
  31.        
  32.         //SetLum(Cs, lumCb)
  33.         float d = lumCb - lumCs;
  34.         fullBlend = cs + d;
  35.        
  36.         //ClipColor()
  37.         float n = min(min(fullBlend.r, fullBlend.g), fullBlend.b);
  38.         float x = max(max(fullBlend.r, fullBlend.g), fullBlend.b);
  39.         if(n < 0.0){
  40.             fullBlend = lumCs + (((fullBlend - lumCs) * lumCs) / (lumCs - n));
  41.         }
  42.        
  43.         if(x > 1.0){
  44.             fullBlend = lumCs + (((fullBlend - lumCs) * (1.0 - lumCs) / (x - lumCs)));
  45.         }
  46.        
  47.         fullBlend.a = cs.a;
  48.        
  49.         dst = mix(cs, fullBlend, percent);
  50.     }
  51. }