0% found this document useful (0 votes)
47 views

Thinking About Prog

The document discusses debugging code to recolor a Duke Blue Devil image to a green devil. It goes through several attempts with different code snippets and conditionals to change only the blue pixels to green. It emphasizes thinking through the problem first before coding, such as considering the RGB values that define blue versus white. The final code snippet uses a conditional that checks if the red value is less than 200 to identify blue pixels and recolor them correctly to green.

Uploaded by

ejohnson07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Thinking About Prog

The document discusses debugging code to recolor a Duke Blue Devil image to a green devil. It goes through several attempts with different code snippets and conditionals to change only the blue pixels to green. It emphasizes thinking through the problem first before coding, such as considering the RGB values that define blue versus white. The final code snippet uses a conditional that checks if the red value is less than 200 to identify blue pixels and recolor them correctly to green.

Uploaded by

ejohnson07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Thinking Critically about

Your Program
A Prelude to Debugging

Previously: Recoloring the Blue Devil


var bb = new SimpleImage("duke_blue_devil.png");
for (var pp of bb.values()) {
pp.setRed(255);
}
print(bb);

var bb = new SimpleImage("duke_blue_devil.png");


for (var pp of bb.values()) {
pp.setRed(255);
pp.setGreen(255);
}
print(bb);

What about a Green Devil?


var bb = new SimpleImage("duke_blue_devil.png");
for (var pp of bb.values()) {
//What goes here?
}
print(bb);

Green devil:
Red = 0, Green = 255, Blue = 100

What about a Green Devil?


var bb = new SimpleImage("duke_blue_devil.png");
for (var pp of bb.values()) {
pp.setRed(0);
pp.setGreen(255);
pp.setBlue(100);
}
print(bb);

All pixels are green now

What about a Green Devil?


var bb = new SimpleImage("duke_blue_devil.png");
for (var pp of bb.values()) {
pp.setGreen(255);
pp.setBlue(100);
}
print(bb);

Foreground: correct, Background: yellow


Need a conditional (if)
If it is blue, then set it to green

What about a Green Devil?


var bb = new SimpleImage("duke_blue_devil.png");
for (var pp of bb.values()) {
if(pixel.getBlue() == 255){
pixel.setRed(0);
pixel.setGreen(255);
pixel.setBlue(100);
}
}
print(bb);

Not what we wanted

Think It Through
White: Green:
R = 255 R = 0
G = 255 G = 255
B = 255 B = 100
var bb = new SimpleImage("duke_blue_devil.png");
for (var pp of bb.values()) {
if(pixel.getBlue() == 255){
pixel.setRed(0);
pixel.setGreen(255);
pixel.setBlue(100);
}
}
print(bb);

Think It Through
Blue:
R = 45
G = 39
B = 225
var bb = new SimpleImage("duke_blue_devil.png");
for (var pp of bb.values()) {
if(pixel.getBlue() == 255){
pixel.setRed(0);
pixel.setGreen(255);
pixel.setBlue(100);
}
}
print(bb);

Think It Through
Blue:
R = 45
G = 39
B = 225

White:
R = 255
G = 255
B = 255

Need condition that distinguishes


red < 200, green < 150, blue != 255

What about a Green Devil?


var bb = new SimpleImage("duke_blue_devil.png");
for (var pp of bb.values()) {
if(pixel.getRed() < 200){
pixel.setRed(0);
pixel.setGreen(255);
pixel.setBlue(100);
}
}
print(bb);

Perfect!
Better: plan first

You might also like