Advertisement
alefhidalgo

ColorsAreBeautiful

Jun 20th, 2011
1,483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.io.IOException;
  3. import java.util.Scanner;
  4. import javax.imageio.ImageIO;
  5.  
  6. /**
  7.  * Tuenti Programming Contest
  8.  * Challenge 14: Colors are beautiful *
  9.  * @author alefhidalgo [at] gmail [dot] com
  10.  */
  11. public class ColorsAreBeautiful {
  12.     private BufferedImage image;
  13.     public static final String IMAGE_RESOURCE = "trabaja.bmp";
  14.     /**
  15.      * Constructor
  16.      * Load the image buffer
  17.      */
  18.     public ColorsAreBeautiful() {
  19.         try {
  20.             image = ImageIO.read(this.getClass().getResource(ColorsAreBeautiful.IMAGE_RESOURCE));
  21.         } catch (IOException e) {
  22.             System.err.println(e.getMessage());
  23.         }
  24.     }
  25.    
  26.     /**
  27.      * Returns the sum of the components R,G or B of the line XYZ
  28.      * @param code RXYZ | GXYZ | BXYZ
  29.      * @return
  30.      */
  31.     public int getSum(String code) {
  32.         int w = image.getWidth();
  33.         char component = code.charAt(0);
  34.         int line = Integer.parseInt(code.substring(1));
  35.  
  36.         int sum = 1;
  37.         for (int j = 0; j < w; j++) {
  38.             int pixel = image.getRGB(j, line);
  39.             switch (component) {
  40.             case 'R':
  41.                 sum += (pixel >> 16) & 0xff;
  42.                 break;
  43.             case 'G':
  44.                 sum += (pixel >> 8) & 0xff;
  45.                 break;
  46.             case 'B':
  47.                 sum += (pixel) & 0xff;
  48.                 break;
  49.             }
  50.         }
  51.         return sum;
  52.  
  53.     }
  54.  
  55.  
  56.     public static void main(String args[]) {
  57.         ColorsAreBeautiful colorsAreBeautiful = new ColorsAreBeautiful();
  58.         Scanner scanner = new Scanner(System.in);  
  59.         while (scanner.hasNextLine()) {
  60.             System.out.println(colorsAreBeautiful.getSum(scanner.nextLine().trim()));
  61.         }
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement