/****************************************************************************
 * This formula file or any formula file derived from it cannot be used for
 * commercial purposes or to create hard copy output without the explicit
 * consent of the original author.
 ****************************************************************************/

fractal {

   // Establish the mapping between complex coordinates
   // and display coordinates

   mapping {
      (0.90749735962996036509,
       -0.24685443786982241843,
       1.03191673110916748435,
       -0.08765917159763300659) => (749,960)
   }

   formula {
     
      z = current;
      zold = [0, 0];

      $xmax = 0; $xmin = 0; $xtot = 0;
      $ymax = 0; $ymin = 0; $ytot = 0;
      $mmax = 0; $mmin = 0; $mtot = 0;

      $trapped   = 0;     
      $stop      = 0;
      $trapDist  = 0;
      $trapValue = .1;
      $tolerance = 1e-6;
      $maxCount  = 10;
    
      while($stop == 0 && $count < $maxCount)
      {
         if(ssq(z - zold) < $tolerance)
         {
            $stop = 1;
         }
         else
         {
            f_z  = pow(z, log(z));
            fp_z = 2 * pow(z, log(z) - 1) * log(z);

            zold = z;
            z    = z - fp_z / f_z;
   
            zValue = pow(z, z);

            $modValue = fmod(abs(real(zValue) - imag(zValue)), 10);

            if($modValue < $trapValue)
            {
               $trapped = 1;
               $stop    = 1;
               $trapDist = abs($modValue - $trapValue);
            }
            else
            {
            value = log(z);

            $x = real(value);
            $y = imag(value);
            $m = mag(value);

            $xtot = $xtot + $x;
            $ytot = $ytot + $y;
            $mtot = $mtot + $m;

            $xmax = max($xmax, $x);
            $xmin = min($xmin, $x);

            $ymax = max($ymax, $y);
            $ymin = min($ymin, $y);

            $mmax = max($mmax, $m);
            $mmin = min($mmin, $m);
            }
         }
      }

      $r = 0;
      $g = 0;
      $b = 0;
              
      if($count > 0)
      {
         $r = (($xtot / $count) - $xmin) / ($xmax - $xmin);
         $g = (($mtot / $count) - $mmin) / ($mmax - $mmin);
         $b = (($ytot / $count) - $ymin) / ($ymax - $ymin);

         if($trapped == 1)
         {
            $factor = $trapDist / $trapValue;

            if(fmod($count, 3) == 1) { $g *= $factor; }
            elseif(fmod($count, 3) == 2) { $r *= $factor; }
            else { $b *= $factor; }
         }
        

         $r = get_sin_color($r, 255, 1);
         $g = get_sin_color($g, 245, .8);
         $b = get_sin_color($b, 255, 1);
      }
      

      set_color($b, $r, $g);
   }
}



























