Letters from Sanford Street # 409

So this was an interesting assignment, basically, I have to take a picture and change the Red, Green, Blue values of the pixels so that each third has a max red, max green or max blue value.

this was my first attempt at solving the issue, it's not technically the correct answer, but I had to do it this way to create a starting point:

var picture = new SimpleImage("hilton.jpg");
 
     for (var pixel of picture.values()) {

        if ((pixel.getX () >= 0  )) if ((pixel.getX () <= 47  )){
        pixel.setRed(255);
        }
        if ((pixel.getX () >= 47 )) if ((pixel.getX () <= 95  )){
        pixel.setGreen(255);
        }
        if ((pixel.getX () >= 95 )) if ((pixel.getX () <= 140 )){
        pixel.setBlue(255);
        }
    }
 
 print(picture)

What I did was manually enter the x-values that would create different 1/3 stripes for the picture, but this wouldn't work, if, say, I was trying to use this code on a different picture with a different width, so the next step is to create a code that automatically creates the 1/3 values, and that's the next step.

var picture = new SimpleImage("hilton.jpg");
 
     for (var pixel of picture.values()) {

        if ((pixel.getX () >= (0 * picture.getWidth() * (1/3))  )) if ((pixel.getX () <= (1 * picture.getWidth() * (1/3))  )){
        pixel.setRed(255);
        }
        if ((pixel.getX () >= (1 * picture.getWidth() * (1/3)) )) if ((pixel.getX () <= (2 * picture.getWidth() * (1/3))  )){
        pixel.setGreen(255);
        }
        if ((pixel.getX () >= (2 * picture.getWidth() * (1/3)) )) if ((pixel.getX () <= (3 * picture.getWidth() * (1/3) ))){
        pixel.setBlue(255);
        }
    }
 
 print(picture)

once it can base the x-coordinates on the actual width, it can be used on any other picture.

var picture = new SimpleImage("eastereggs.jpg");
 
     for (var pixel of picture.values()) {

        if ((pixel.getX () >= (0 * picture.getWidth() * (1/3))  )) if ((pixel.getX () <= (1 * picture.getWidth() * (1/3))  )){
        pixel.setRed(255);
        }
        if ((pixel.getX () >= (1 * picture.getWidth() * (1/3)) )) if ((pixel.getX () <= (2 * picture.getWidth() * (1/3))  )){
        pixel.setGreen(255);
        }
        if ((pixel.getX () >= (2 * picture.getWidth() * (1/3)) )) if ((pixel.getX () <= (3 * picture.getWidth() * (1/3) ))){
        pixel.setBlue(255);
        }
    }
 
 print(picture)



* * *

var picture = new SimpleImage("eastereggs.jpg");
 
     for (var pixel of picture.values()) {

        if ((pixel.getX () >= (0 * picture.getWidth() * (1/3)) )) if ((pixel.getX () <= (1 * picture.getWidth() * (1/3)) )){
        pixel.setRed(255);
        }
        if ((pixel.getX () >= (1 * picture.getWidth() * (1/3)) )) if ((pixel.getX () <= (2 * picture.getWidth() * (1/3)) )){
        pixel.setGreen(255);
        }
        if ((pixel.getX () >= (2 * picture.getWidth() * (1/3)) )) if ((pixel.getX () <= (3 * picture.getWidth() * (1/3)) )){
        pixel.setBlue(255);
        }
    }
 
 print(picture)


* * *

I tried to simplify the code, but this didn't work.

 var picture = new SimpleImage("hilton.jpg");
 
     for (var pixel of picture.values()) {

        if ((((1 * picture.getWidth() * (1/3)))  <= (pixel.getX ()) >= ((0 * picture.getWidth() * (1/3)))   )){
        pixel.setRed(255);
        }
        if ((((2 * picture.getWidth() * (1/3)))  <= (pixel.getX ()) >= ((1 * picture.getWidth() * (1/3)))   )){
        pixel.setGreen(255);
        }
        if ((((3 * picture.getWidth() * (1/3))) <= (pixel.getX ()) >= ((2 * picture.getWidth() * (1/3)))    )){
        pixel.setBlue(255);
        }
    }
 
 print(picture)

No comments:

Post a Comment