Thursday, 7 July 2016

Suppose we decide to write a function to set the color of a pixel by giving it three RGB values. Think about what the parameters and the code would need to be for this function. Let’s call the function setColor. Which one of the following is a correct setColor function?

// write your code here
function clearbits(colorval){
    //compute the same color value with the lowest bits zeroed
    var x = Math.floor(colorval/16)*16;
    return x;
}
function setColor( r, g, b) {
    for (var pixel of start.values()){
        pixel.setRed(r);
     pixel.setGreen(g);
   pixel.setBlue(b);
    }
return start;
}
function chop2hide(image){
    //for each pixel in the image
    for (var px of image.values()){
        //clear the lower values of red
        px.setRed(clearbits(px.getRed()));
        //clear the lower values of green
        px.setGreen(clearbits(px.getGreen()));
        //clear the lower values of blue
        px.setBlue(clearbits(px.getBlue()));
    }
    return image;
}
function shift(image){
    //for each pixel in an image
    for (var px of image.values()){
        //shift the red bits over
        px.setRed(px.getRed()/16);
        //shift the Green bits over
        px.setGreen(px.getGreen()/16);
        //shift the Blue bits over
        px.setBlue(px.getBlue()/16);
    }
    return image;
}
function open(imagetoopen){
    for (var px of imagetoopen.values()){
    var or = px.getRed();
    var og = px.getGreen();
    var ob = px.getBlue();
    px.setRed((or - (clearbits(or)))+16);
    px.setGreen((og - (clearbits(og)) )+16);
    px.setBlue((ob - (clearbits(ob)))+16);
    }

    return imagetoopen;
}  
function combine(start,Hide){
    //make a new image of the same size as SHOW (Call it Answer)
    var answer = new SimpleImage(start.getWidth() , start.getHeight());
    //for every pixel of new image
    for (var px of answer.values()){
        var x = px.getX();
        var y = px.getY();
        //get the pixel in the same place from show
        var startPixel = start.getPixel(x,y);
        //get the pixel in the same place from hide
        var hidePixel = hide.getPixel(x,y);
        //set the red pixel to the sum of the show pixel red + hide pixel red
        px.setRed(startPixel.getRed() + hidePixel.getRed());
        //set the Green pixel to the sum of the show pixel Green + hide pixel Green
        px.setGreen(startPixel.getGreen() + hidePixel.getGreen());
        //set the Blue pixel to the sum of the show pixel Blue + hide pixel Blue
        px.setBlue(startPixel.getBlue() + hidePixel.getBlue());
    }
    return answer;
}
var start = new SimpleImage("test2.png");
var newcolor = setColor(232,12,20)
//var Hide = new SimpleImage("drewRobert.png");
//start = chop2hide(start);
//hide = shift(Hide);
//var answer = combine (start,hide);

//print (answer);
//var undo =  open(start);
//print (undo);
print (newcolor);

No comments:

Post a Comment