// (c) MIT 2003.  All rights reserved.

// $Id: Colors.java,v 1.6 2003/03/06 00:40:40 moltmans Exp $
// Time-stamp: <2000-04-22 17:36:22 moltmans>
package rec.sys;


import java.awt.*;
import java.util.*;
import rec.util.*;

public class Colors {
  
  public static Color STROKE = Color.gray;

  public static Color BACKGROUND = Color.white;

  public static Color SELECTED = Color.red;

  public static Color TEXT = Color.gray;

  /**
   * Maps Strings of color names to colors.
   */
  private static Hashtable colors;

  private static boolean fade_up;

  static {
    colors = new Hashtable();
    colors.put( "stroke", STROKE );
    colors.put( "background", BACKGROUND );
    colors.put( "selected", SELECTED );
    colors.put( "text", TEXT );

    colors.put( "black", Color.black );
    colors.put( "white", Color.white );
    colors.put( "blue", Color.blue );
    colors.put( "green", Color.green );
    colors.put( "grey", Color.gray );
    colors.put( "gray", Color.gray );
    colors.put( "yellow", Color.yellow );
    colors.put( "orange", Color.orange );
    colors.put( "red", Color.red );
    colors.put( "cyan", Color.cyan );
    colors.put( "purple", Color.magenta );
    colors.put( "magenta", Color.magenta );
  }

  
  public static void setColor( String name, Color c )
  {
    colors.put( name, c );
    
    if ( name.equals( "background" ) )
      {
	setFade();
      }
  }

  /**
   * Only set the color if it has not been set already.
   */
  public static void setDefaultColor( String name, String c )
  {
    if ( getColor( name ) == null )
      {
	setColor( name, c );
      }
  }

  public static void setDefaultColor( String name, Color c )
  {
    if ( getColor( name ) == null )
      {
	setColor( name, c );
      }
  }


  private static void setFade( )
  {
    Color c = getBackground();
    
    int v = (int)( 0.3 * c.getBlue() + 0.4 * c.getRed() + 0.3 * c.getGreen() );

    Console.writeln( "fade check gives " + v );
    if ( v < 150 )
      fade_up = false;
    else
      fade_up = true;
  }

  public static void setColor( String name, String colorname )
  {
    Console.writeln( "Setting color " + name + " to " + colorname );
    //    setColor( name, getColor( colorname ) );
    colors.put( name, colorname );

    if ( name.equals( "background" ) )
      {
	setFade();
      }
  }

  public static Color getBackground( )
  {
    return getColor( "background" );
  }

  public static Color getColor( String name )
  {
    name = name.toLowerCase();
    
    Object o = colors.get( name );

    int bottom_counter = 0;
    // find an actual color.
    while( ( bottom_counter++ < 100) 
	   && ( o != null ) 
	   && (o instanceof String) )
      o = colors.get( o );
    
    Color c = (Color)o;

    if ( c == null )
      return null;

    return c;
  }

  /**
   * @get something which fades into the background.
   */
  public static Color getFade( String name )
  {
    Color c = getColor( name );

    if ( c == null )
      return null;

    return fade( c );
  }

  public static Color fade( Color c )
  {
    return fade( c, 2 );
  }

  public static Color fade( Color c, int lvl )
  {
    /*    if ( fade_up )
      return c.brighter().brighter();
    else
      return c.darker().darker();
      */

    Color bk = getBackground();
    
    int rd = 2 * c.getRed() + lvl * bk.getRed();
    int gr = 2 * c.getGreen() + lvl * bk.getGreen();
    int bl = 2 * c.getBlue() + lvl * bk.getBlue();

    return new Color( rd / (2 + lvl), gr / (2+lvl), bl / (2+lvl) );
  }

  public static Color brighten( Color c )
  {
    Color bk = getBackground();
    
    int dr = c.getRed() - bk.getRed();
    int dg = c.getGreen() - bk.getGreen();
    int db = c.getBlue() - bk.getBlue();

    dr = Math.max( Math.min( c.getRed() + dr / 3, 255 ), 0 );
    dg = Math.max( Math.min( c.getGreen() + dg / 3, 255 ), 0 );
    db = Math.max( Math.min( c.getBlue() + db / 3, 255 ), 0 );

    return new Color( dr, dg, db );
  }
}
