Create a Border With Functions

Solve It

Again we are framing an image with black edges. This example extends our use of functions.

Code It

// border frame using a function, refactored function setBlack(pixel){ pixel.setRed(0); pixel.setGreen(0); pixel.setBlue(0); return pixel; } function pixelOnEdge(pixel,image){ var x = pixel.getX(); var y = pixel.getY(); if (x < 10) return true; if (y < 10) return true; if (x >= image.getWidth() - 10) return true; if (y >= image.getHeight() - 10) return true; return false; } var image = new SimpleImage("lion.jpg"); for (var pixel of image.values()) { if (pixelOnEdge(pixel,image)){ pixel = setBlack(pixel); } } print(image);

See It

 
 

Available Images


lion.jpg
[250x188]

Drop your images onto the area above to make it available within your code editor on this page. Note: your images will not be uploaded anywhere, they will stay on your computer.