ColorSeparation.java


Below is the syntax highlighted version of ColorSeparation.java from §3.1 Using Data Types.

/*************************************************************************
 *  Compilation:  javac ColorSeparation.java
 *  Execution:    java ColorSeparation filename
 * 
 *  Reads in an image from a file, and displays the red, green, and
 *  blue portions in three separate windows.
 *
 *  % java ColorSeparation baboon.jpg
 *
 *************************************************************************/

import java.awt.Color;

public class ColorSeparation {

    public static void main(String args[]) {
        Picture pic = new Picture(args[0]);
        int width  = pic.width();
        int height = pic.height();

        Picture R = new Picture(width, height);
        Picture G = new Picture(width, height);
        Picture B = new Picture(width, height);

        // separate colors
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                Color c = pic.getColor(i, j);
                int r = c.getRed();
                int g = c.getGreen();
                int b = c.getBlue();
                R.setColor(i, j, new Color(r, 0, 0));
                G.setColor(i, j, new Color(0, g, 0));
                B.setColor(i, j, new Color(0, 0, b));
            }
        }

        R.show();
        G.show();
        B.show();
        R.save("r.jpg");
        G.save("g.jpg");
        B.save("b.jpg");
    }
 

   
}


Last updated: Fri Oct 15 23:15:39 EDT 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.