package yi.iu.processor.color;

/**
 * This class is mainly to used to represent the color info of lines
 * @author Yi Li
 * @version 1.0
 */
public class ColorPair {
  int m_color1, m_color2;
  public ColorPair(int color1, int color2) {
    m_color1 = color1; m_color2 = color2;
  }

  /**
   * <p>
   * Given the color pair index, return the color pair.  The color component of
   * the color pair is 0 based and it is index color instead of true color.  The
   * color pair index is also 0 based.  The first color component is always
   * smaller than the second one.  Eg. color_num = 5;
   * </p>
   *
   * <pre>
   * 0 -> (0,0)
   * 1 -> (0,1)
   * ...
   * 4 -> (0,4)
   * 5 -> (0,1)
   * ...
   * </pre>
   *
   * @param color Color pair index
   * @param color_num The total number of the colors.
   * @return The color pair
   */
  public static ColorPair index2Pairs(int color_index, int color_num)
  {
      int c1= color_index / color_num;
      int c2= color_index % color_num;
      if (c1 < c2) return new ColorPair(c2, c1);
      else return new ColorPair(c2, c1);
  }

  /**
   * Given a color pair, return an 1-D index for it.  The color component of
   * the color pair is 0 based and it is index color instead of true color.  The
   * color pair index is also 0 based.  The first color component is always
   * smaller than the second one.  Eg. color_num = 5;
   * </p>
   *
   * <pre>
   * (0,0) -> 0
   * (0,1) -> 1
   * ...
   * (0,4) -> 4
   * (1,0) -> 1
   * ...
   * </pre>
   *
   * @param color_pair a color pair
   * @param color_num the total number of colors
   * @return color pair index
   */
  public static int pair2Index(ColorPair color_pair, int color_num)
  {
    if (color_pair.m_color1 < color_pair.m_color2)
      return color_pair.m_color1 * color_num + color_pair.m_color2;
    else
      return color_pair.m_color2 * color_num + color_pair.m_color1;
  }
}