/****************************************************************************
 * 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 {
      (-1.20500568828213960000,
       -2.19022222222222230000,
       3.23686006825938580000,
       2.22933333333333380000) => (963,960)
   }





   formula
   {
      z          = current;
      c          = [0, 0];
      $stop      = 0;
      $xMin      = 0;
      $xMax      = 0;
      $xTot      = 0;
      $yMin      = 0;
      $yMax      = 0;
      $yTot      = 0;
      $mMax      = 0;
      $mMin      = 0;
      $mTot      = 0;

      while($stop == 0 && $count < 20)
      {
         $x = real(z);
         $y = imag(z);
         $m = mag(z);

         $xTot = $xTot + $x; $xMin = min($xMin, $x); $xMax = max($xMax, $x);
         $yTot = $yTot + $y; $yMin = min($yMin, $y); $yMax = max($yMax, $y);
         $mTot = $mTot + $m; $mMin = min($mMin, $m); $mMax = max($mMax, $m);

         $d = fmod(old_deg(z), 90);

         if($count > 0 && $d > 40 && $d < 50)
         {
            $stop = 1;
         }
         else
         {
            z = log(pow(z * log(z), 2.5));
            c = c * z;
         }
      }

      $r = 0;
      $g = 0;
      $b = 0;

      if($count > 0)
      {
         $xValue = (($xTot / $count) - $xMin) / ($xMax - $xMin);
         $yValue = (($yTot / $count) - $yMin) / ($yMax - $yMin);
         $mValue = (($mTot / $count) - $mMin) / ($mMax - $mMin);
         
         // some constants ...

         $pi    = 3.14159265358979323846;
         $twopi = 2 * $pi;

         // adjust these to taste ... they should be: 0 <= value <= 255

         $rStart = 255;
         $gStart = 255;
         $bStart = 255;

         // adjust these to taste ... they should be: 0 <= value <= 2

         $rStep = 2;
         $gStep = 1;
         $bStep = 1;

         // Calculate the colors ...

         $r = -cos($twopi * $xValue);
         $g = -cos($twopi * $yValue);
         $b = -cos($twopi * $mValue);

         $r = $r * 127 + 127;
         $g = $g * 127 + 127;
         $b = $b * 127 + 127;

         $r = $r * $rStep + $rStart;
         $g = $g * $gStep + $gStart;
         $b = $b * $bStep + $bStart;

         $r = $r / 360;
         $g = $g / 360;
         $b = $b / 360;

         $r = sin($pi * $r);
         $g = sin($pi * $g);
         $b = sin($pi * $b);

         $r = $r * 128 + 127;
         $g = $g * 128 + 127;
         $b = $b * 128 + 127;


        
      }

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













