package control;
import java.awt.Color;

/**
 * A brush is a color and a style of painting (opaque or not).
 * A brush that is opaque will fill the content of the drawing.
 */
public class Brush {
  final Color color;
  // corresponding opaque brush, null if the corresponding opaque brush is not computed yet
  private Brush opaqueBrush;

  private Brush(Color color, boolean opaque) {
    this.color = color;
    this.opaqueBrush = (opaque)? this: null;
  }

  private Brush(int red, int green, int blue, boolean opaque) {
    this(new Color(
        checkColorComponent(red),
        checkColorComponent(green),
        checkColorComponent(blue)),
        opaque);
  }

  private static int checkColorComponent(int component) {
    if (component < 0 || component > 255) {
      throw new IllegalArgumentException("bad component value " + component);
    }
    return component;
  }

  /**
   * Creates a new Brush representing a color.
   * 
   * @param red red component of the color of the brush.
   *        the value must be between 0 and 255.
   * @param green green component of the color of the brush.
   *        the value must be between 0 and 255.
   * @param blue blue component of the color of the brush.
   *        the value must be between 0 and 255.
   */
  public Brush(int red, int green, int blue) {
    this(red, green, blue, false);
  }

  /**
   * Returns true if the current brush is opaque.
   * @return true if the current brush is opaque.
   */
  public boolean isOpaque() {
    return opaqueBrush == this;
  }

  /**
   * Returns a brush with the same color components and a style opaque.
   * @return a brush with the same color components and a style opaque.
   */
  public Brush asOpaque() {
    if (opaqueBrush != null) {
      return opaqueBrush;
    }
    return opaqueBrush = new Brush(color, true);
  }

  /**
   * The brush corresponding to the color red.
   */
  public static final Brush RED = new Brush(255, 0, 0);

  /**
   * The brush corresponding to the color green.
   */
  public static final Brush GREEN = new Brush(0, 255, 0);

  /**
   * The brush corresponding to the color blue.
   */
  public static final Brush BLUE = new Brush(0, 0, 244);

  /**
   * The brush corresponding to the color light gray.
   */
  public static final Brush LIGHT_GRAY = new Brush(192, 192, 192);

  /**
   * The brush corresponding to the color gray.
   */
  public static final Brush GRAY = new Brush(128, 128, 128);

  /**
   * The brush corresponding to the color drak gray.
   */
  public static final Brush DARK_GRAY = new Brush(64, 64, 64);

  /**
   * The brush corresponding to the color black.
   */
  public static final Brush BLACK = new Brush(0, 0, 0);

  /**
   * The brush corresponding to the color pink.
   */
  public static final Brush PINK = new Brush(255, 175, 175);

  /**
   * The brush corresponding to the color orange.
   */
  public static final Brush ORANGE = new Brush(255, 200, 0);

  /**
   * The brush corresponding to the color yellow.
   */
  public static final Brush YELLOW = new Brush(255, 255, 0);

  /**
   * The brush corresponding to the color magenta.
   */
  public static final Brush MAGENTA = new Brush(255, 0, 255);

  /**
   * The brush corresponding to the color cyan.
   */
  public static final Brush CYAN = new Brush(0, 255, 255);

  /**
   * The brush corresponding to the color white.
   */
  public static final Brush WHITE = new Brush(255, 255, 255);
}