=// method to make a 5 color posterized version of a picture=
=public static Picture q21 (Picture p)=
={=
=int xPos, yPos;=
=int wid = p.getWidth();=
=int hgt = p.getHeight();=
=// loop for all the x values=
=for ( xPos = 0 ; xPos < wid ; ++xPos )=
={=
=// loop for the the Y values=
=for ( yPos = 0 ; yPos < hgt ; ++yPos )=
={=
=// access a pixel=
=Pixel pix = p.getPixel (xPos, yPos);=
=// access the color values of that pixel=
=int red = pix.getRed ();=
=int green = pix.getGreen ();=
=int blue = pix.getBlue ();=
=// modify the color values of the pixel=
=int grayAmount;=
=grayAmount = (int)Math.round((red * 0.299) + (green * 0.587) + (blue * 0.114)) ; if ( grayAmount < 255 / 5 ) // 51=
={ pix.setColor ( Color.BLACK );=
=}=
=else if ( grayAmount < 2 * 255 / 5 ) // 102=
={ pix.setColor ( Color.BLUE ); }=
=else if ( grayAmount < 3 * 255 / 5 ) // 153=
={ pix.setColor ( Color.GREEN ); }=
=else if ( grayAmount < 4 * 255 / 5 ) // 204=
={ pix.setColor ( Color.CYAN );=
=}=
=else=
={ pix.setColor ( Color.WHITE); }=
=}=
=}=
=return p;=
=}=
22.
=// method to make a color negtive from a picture=
Copyright 2016 The Board of Trustees of the University of Illinois.webmaster@cs.uic.edu |
WISEST Helping Women Faculty Advance Funded by NSF | ![]() | ![]() |