public class SimpleRGB { // TO DO: Instant Variables private int width; private int height; private int[] reds; private int[] blues; private int[] greens; public SimpleRGB(int aWidth, int aHeight) { this.width = aWidth; this.height = aHeight; reds = new int[width*height]; blues = new int[width*height]; greens = new int[width*height]; } /** * Gets the width of this image. * @return the width of this image. */ public int getWidth() { return this.width; } /** * Gets the height of this image. * @return the height of this image. */ public int getHeight() { return this.height; } /** * Sets the red value at coordinate (x,y) to aRed. * @param x the x coordinate of this image. * @param y the y coordinate of this image. * @param aRed the red value (0 - 255) */ public void setRed(int x, int y, int aRed) { reds[(y*this.width) + x] = aRed; } /** * Sets the green value at coordinate (x,y) to aGreen. * @param x the x coordinate of this image. * @param y the y coordinate of this image. * @param aGreen the green value (0 - 255) */ public void setGreen(int x, int y, int aGreen) { greens[(y*this.width)+x] = aGreen; } /** * Sets the blue value at coordinate (x,y) to aBlue. * @param x the x coordinate of this image. * @param y the y coordinate of this image. * @param aBlue the blue value (0 - 255) */ public void setBlue(int x, int y, int aBlue) { blues[(y*this.width)+x] = aBlue; } /** * Gets the red value at coordinate (x,y). * @param x the x coordinate of this image. * @param y the y coordinate of this image. * @return the value of red at coordinate (x,y). */ public int getRed(int x, int y) { return reds[(y*this.width) + x]; } /** * Gets the green value at coordinate (x,y). * @param x the x coordinate of this image. * @param y the y coordinate of this image. * @return the value of green at coordinate (x,y). */ public int getGreen(int x, int y) { return greens[(y*this.width)+x]; } /** * Gets the blue value at coordinate (x,y). * @param x the x coordinate of this image. * @param y the y coordinate of this image. * @return the value of blue at coordinate (x,y). */ public int getBlue(int x, int y) { return blues[(y*this.width) + x]; } /** * Get the NEW image containing only the red color. * The red values of this new image should be exactly * the same as red value of this image. The green and * blue values of this new image should be 0s. * @return the NEW image (SimpleRGB) containing only * the red color of this image. */ public SimpleRGB getRedImage() { SimpleRGB output = new SimpleRGB(this.width,this.height); for(int i = 0; i < reds.length; i++) { output.setRed(i%this.width,(i/this.width),reds[i]); } return output; } /** * Get the NEW image containing only the green color. * The green values of this new image should be exactly * the same as green value of this image. The red and * blue values of this new image should be 0s. * @return the NEW image (SimpleRGB) containing only * the green color of this image. */ public SimpleRGB getGreenImage() { SimpleRGB output = new SimpleRGB(this.width,this.height); for(int i = 0; i < greens.length; i++) { output.setGreen(i%this.width,(i/this.width),greens[i]); } return output; } /** * Get the NEW image containing only the blue color. * The blue values of this new image should be exactly * the same as blue value of this image. The red and * green values of this new image should be 0s. * @return the NEW image (SimpleRGB) containing only * the blue color of this image. */ public SimpleRGB getBlueImage() { SimpleRGB output = new SimpleRGB(this.width,this.height); for(int i = 0; i < blues.length; i++) { output.setBlue(i%this.width,(i/this.width),blues[i]); } return output; } /** * Get the NEW image representing the greyscale of this * image. The grey colors are colors that the red, green * and blue value are exactly the same. To convert an RGB * image into a greyscale image, use the following formula * to calculate the new value. * (0.21 * red) + (0.72 * green) + (0.07 * blue) * For example, suppose the (R,G,B) value of this image at * coordinate (10,20) are (10,100,200), since * (0.21 * 10) + (0.72 * 100) + (0.07 * 200) = 88 * the (R,G,B) value of the new greyscale image at (10,20) * should be (88,88,88). * @return the NEW image representing the greyscale of this * image. */ /** *We're not british! *It's grEy in England, *and grAy in America! */ public SimpleRGB getGreyImage() { SimpleRGB output = new SimpleRGB(this.width,this.height); for(int i = 0; i < reds.length; i++) { int gray = (int) ((reds[i]*0.21) + (greens[i]*0.72) + (blues[i]*0.07)); output.setRed(i%this.width,(i/this.width),gray); output.setGreen(i%this.width,(i/this.width),gray); output.setBlue(i%this.width,(i/this.width),gray); } return output; } }