Conditionals part 3 Barb Ericson Georgia Institute of
Conditionals – part 3 Barb Ericson Georgia Institute of Technology Nov 2009 Conditionals-part 3 1
Learning Goals • Understand at a conceptual and practical level – How to use conditionals with > 2 possibilities – How to sepia-tint a picture – How to test for values in ranges – How to posterize a picture Conditionals-part 3 2
Sepia-Toned Pictures • Have a yellowish tint, used to make things look old and western Conditionals-part 3 3
Sepia-toned Algorithm • First make the picture grayscale. • Loop through all the pixels in the picture – Change the shadows (darkest grays) to be even darker (red >= 0 && red < 60) – Make the middle grays a brown color (60 <= red < 190) – Make the highlights (lightest grays) a bit yellow (red >= 190) • Increase red and green • Or decrease blue Conditionals-part 3 4
Using Multiple If Statements • If we are doing different things based on a set of ranges – 0 <= x <= 5 – 5 < x <= 10 – 10 < x if (0 <= x && x <= 5) Statement or block if (5 < x && x <= 10) Statement or block if (10 < x) Statement or block Conditionals-part 3 5
Using “else if” for > 2 Options • If we are doing different things based on a set of ranges – 0 <= x <= 5 – 5 < x <= 10 – 10 < x if (0 <= x && x <= 5) Statement or block else if (x <= 10) Statement or block else • You don’t need to check if x > 5 since the first if block would have executed if it was Statement or block Conditionals-part 3 6
Conditionals with > 2 Choices if (0 <= x && x <= 5) { } else if (x <= 10) { } else // what is x? { } Conditionals-part 3 7
Sepia-toned Method public void sepia. Tint() { Pixel pixel. Obj = null; double red. Value = 0; double green. Value = 0; double blue. Value = 0; // first change the current picture to grayscale this. grayscale(); Conditionals-part 3 8
Sepia-toned Method - Cont // loop through the pixels for (int x = 0; x < this. get. Width(); x++) { for (int y = 0; y < this. get. Height(); y++) { // get the current pixel and color values pixel. Obj = this. get. Pixel(x, y); red. Value = pixel. Obj. get. Red(); green. Value = pixel. Obj. get. Green(); blue. Value = pixel. Obj. get. Blue(); Conditionals-part 3 9
Sepia-toned Method - Cont // tint the shadows darker if (red. Value < 60) { red. Value = red. Value * 0. 9; green. Value = green. Value * 0. 9; blue. Value = blue. Value * 0. 9; } // tint the midtones a light brown by reducing the blue else if (red. Value < 190) { blue. Value = blue. Value * 0. 8; } Conditionals-part 3 10
Sepia-toned Method - Cont // tint the highlights a light yellow // by reducing the blue else { blue. Value = blue. Value * 0. 9; } // set the colors pixel. Obj. set. Red((int) red. Value); pixel. Obj. set. Green((int) green. Value); pixel. Obj. set. Blue((int) blue. Value); } } } Conditionals-part 3 11
Testing sepia. Tint String file = File. Chooser. get. Media. Path("gorge. jpg"); Picture p = new Picture(file); p. explore(); p. sepia. Tint(); p. explore(); Conditionals-part 3 12
Posterize • Reducing the number of different colors in an image • Set all values in a range to one value (the midpoint of the range) – – Set all < 64 to 31 Set all <128 to 95 Set all < 192 to 159 Set the rest to 223 Conditionals-part 3 13
Posterize Algorithm • Loop through all the pixels in an image – Get the red value for the pixel • Find the right range and set the new red value – Get the green value for the pixel • Find the right range and set the new green value – Get the blue value for the pixel • Find the right range and set the new blue value Conditionals-part 3 14
Posterize Exercise • Write the method posterize() in the Picture. java class – that will reduce the number of colors by changing color values to 1 of 4 values • • Set all from 0 to 63 to 31 (< 64) Set all from 64 to 127 to 95 (< 128) Set all from 128 to 191 to 159 (< 192) Set all from 192 to 255 to 223 • Test with tammy. jpg Conditionals-part 3 15
Summary • Use if, else if, and else for > 2 possiblities – Add additional else if’s as needed • To sepia-tint a picture – Change it to grayscale – Make the shadows darker – Make the middle grays brown – Make the lightest grays yellow • To posterize a picture – Change each color to the center of a range of colors Conditionals-part 3 16
- Slides: 16