Advertisement
khAldr0g0

saturation

Dec 18th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.     function saturate($adjust){
  3.         $img=$this->image;
  4.         $nc=imageColorsTotal($img);
  5.        
  6.         $s=$adjust;
  7.  
  8.         //$lumR= 0.2125; $lumG= 0.7154; $lumB= 0.0721;
  9.         $lumR = 0.3086; $lumG= 0.6094; $lumB= 0.0820;
  10.  
  11.         $d=1.0-$s;
  12.  
  13.         $sr=$d*$lumR; $sg=$d*$lumG; $sb=$d*$lumB;
  14.  
  15.         $a0=$sr+$s; $a1=$sg+$s; $a2=$sb+$s;
  16.  
  17.         $ar=array([$a0,$sr,$sr,0,0],[$sg,$a1,$sg,0,0],[$sb,$sb,$a2,0,0],[0,0,0,1.0,0]);
  18.  
  19.         $result=function ($r,$g,$b,$a) use($ar){
  20.             for($i=0; $i<=4; $i++){
  21.                 $colors[$i] =($ar[0][$i]*$r)+($ar[1][$i]*$g)+($ar[2][$i]*$b)+($ar[3][$i]*$a);
  22.             }
  23.             return $colors;
  24.         }; 
  25.        
  26.         $clamp=function($val)
  27.             {
  28.             return (($val>255)?255 : (($val<0)?0 :$val));
  29.         };
  30.  
  31.         for($x=0; $x<$nc; $x++){
  32.             list($r,$g,$b,$a)=array_values(imageColorsForIndex($img,$x));
  33.             $color=$result($r/255,$g/255,$b/255,$a);
  34.  
  35.             $nr=$clamp($color[0]*255);
  36.             $ng=$clamp($color[1]*255);
  37.             $nb=$clamp($color[2]*255);
  38.             $na=$color[3];
  39.  
  40.             imageColorSet($img,$x,$nr,$ng,$nb,$na);
  41.         }
  42.         return $img;
  43.     }
  44. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement