/**
  * Checks if image should be recolored and resized or only resized.
  *
  * @param img - image
  * @param w - width to set
  * @param h - height to set
  * @param name - file name
  * @return
  */
 public static BufferedImage resetTheImage(BufferedImage img, int w, int h, String name) {
   if (name.contains("dnrc") || name.contains("DNRC")) {
     if (w != 0 && h != 0) {
       return resizeImage(img, w, h);
     } else if (img.getWidth() > Config.getWidth() - 20) {
       return resizeImage(img, Config.getWidth() - 20, Config.getHeight());
     }
   } else {
     return recolorImage(img, w, h);
   }
   return null;
 }
  /**
   * Recalculates the color of pixel to our convetion. For example we want green console, so image
   * needs to be in green scale, so this does the trick
   *
   * @param r - red channel
   * @param g - green channel
   * @param b - blue channel
   * @return new color
   */
  private static Color transformColor(int r, int g, int b) {
    int r1, g1, b1;
    double[] a =
        calcDiff(
            Config.getFrontgroundColor().getRed(),
            Config.getFrontgroundColor().getGreen(),
            Config.getFrontgroundColor().getBlue());

    r1 = (int) Math.ceil(r * a[0]);
    g1 = (int) Math.ceil(g * a[1]);
    b1 = (int) Math.ceil(b * a[2]);

    return new Color(r1, g1, b1);
  }
  /**
   * Resizes image to width equal to console width-20 and image height
   *
   * @param img - image to resize
   * @return resized image
   */
  public static BufferedImage resizeImage(BufferedImage img) {
    BufferedImage out =
        new BufferedImage(Config.getWidth() - 20, Config.getHeight(), img.getType());
    Graphics2D g = out.createGraphics();

    g.setRenderingHint(
        RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.drawImage(
        img,
        0,
        0,
        Config.getWidth() - 20,
        img.getHeight(),
        0,
        0,
        img.getWidth(),
        img.getHeight(),
        null);
    g.dispose();

    return out;
  }
  /**
   * This method recolors and resizes image. Recoloring is done using math:
   * r*(frontground_Red/255)...
   */
  public static BufferedImage recolorImage(BufferedImage img, int w, int h) {
    int kolor, red, green, blue;
    BufferedImage out;
    BufferedImage resized = img;

    if (w != 0 && h != 0) {
      out = new BufferedImage(w, h, img.getType());
    } else {
      out = new BufferedImage(Config.getWidth() - 20, Config.getHeight(), img.getType());
    }

    /*
     * Check if the image is too width, else we ignore it.
     */
    if (w != 0 && h != 0) {
      resized = resizeImage(img, w, h);
    } else if (img.getWidth() > Config.getWidth() - 20) {
      resized = resizeImage(img, Config.getWidth() - 20, Config.getHeight());
    }

    Graphics2D g = out.createGraphics();

    for (int x = 0; x < resized.getWidth(); x += getSkipPixels()) {
      for (int y = 0; y < resized.getHeight(); y += getSkipPixels()) {
        kolor = resized.getRGB(x, y);
        red = (kolor & 0x00ff0000) >> 16;
        green = (kolor & 0x0000ff00) >> 8;
        blue = kolor & 0x000000ff;
        g.setColor(transformColor(red, green, blue));
        g.fillRect(x, y, getSkipPixels(), getSkipPixels());
      }
    }
    g.dispose();

    return out;
  }
  /**
   * Counts how many lines (one line = Width/fontWidth chars) is in panel and adds new lines
   *
   * @param text
   * @return
   */
  public static int countLines(String text, int lines) {

    return (int)
            (Math.ceil((double) text.length() / ((Config.getWidth() - 20) / Config.getFontWidth())))
        + lines;
  }