/*
 * FadeSpec.js
 * Copyright (c) 2003 Paul Sturm, all rights reserved.  For authorized use only.
 * sturm@branewave.com
 *
 * $Header: /home/sturm/js/RCS/FadeSpec.js,v 1.2 2003/05/06 14:40:23 sturm Exp $
 *
 */


function FadeSpec_sqrt( frac )
{
   return Math.sqrt( frac );
}


function FadeSpec_square( frac )
{
   return frac * frac;
}


function FadeSpec( reps, arg1, arg2, arg3, arg4 )
{
   this.reps = ( reps == undefined ) ? 1 : reps;
   this.fades = [ ];
   this.shaper = function( frac ) { return frac };


   this.addFade = function( fade )
   {
      this.fades.push( fade );
   }


   if ( arg1.constructor == Array )
   {
      var fades = arg1;
      for ( var i = 0; i < fades.length; i ++ ) this.addFade( fades[ i ] );
   } else {
      this.addFade( { prop: arg1, unit: arg2, start: arg3, finish: arg4 } );
   }


   this.setShaper = function( shaper )
   {
      this.shaper = shaper;
   }


   this.styleStep = function( style, step )
   {
      if ( step < 0 ) step = 0;
      if ( step > this.reps ) step = this.reps;

      for ( var i = 0; i < this.fades.length; i ++ )
      {
         var fade = this.fades[ i ];
         var frac = this.shaper( step / this.reps );
         var value = this.mix( fade, frac );

         style[ fade.prop ] = this.specToString( value, fade.unit );
      }
   }


   this.mix = function( fade, frac )
   {
      if ( fade.unit == "RGB" )
         return RGB_mix( fade.start, fade.finish, frac );
      else
         return ( 1 - frac ) * fade.start + frac * fade.finish;
   }


   this.specToString = function( value, unit )
   {
      return ( unit == "RGB" ) ?  value.toString( ) : "" + value + unit;
   }
}
