# Rainbow Filter

### Solve It

We will use a similar idea to the red filter, but using a filter based around a different color for each stripe.

### Code It

function filterRainbow(image) { var height = image.getHeight(); for (var pixel of image.values()) { var y = pixel.getY(); var avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue()) / 3; if (y < height / 7) { //red if (avg < 128) { pixel.setRed(2 * avg); pixel.setGreen(0); pixel.setBlue(0); } else { pixel.setRed(255); pixel.setGreen(2 * avg - 255); pixel.setBlue(2 * avg - 255); } } else if (y < height * 2 / 7) { //orange if (avg < 128) { pixel.setRed(2 * avg); pixel.setGreen(0.8*avg); pixel.setBlue(0); } else { pixel.setRed(255); pixel.setGreen(1.2*avg-51); pixel.setBlue(2 * avg - 255); } } else if (y < height * 3 / 7) { //yellow if (avg < 128) { pixel.setRed(2 * avg); pixel.setGreen(2*avg); pixel.setBlue(0); } else { pixel.setRed(255); pixel.setGreen(255); pixel.setBlue(2 * avg - 255); } } else if (y < height * 4 / 7) { //green if (avg < 128) { pixel.setRed(0); pixel.setGreen(2*avg); pixel.setBlue(0); } else { pixel.setRed(2*avg-255); pixel.setGreen(255); pixel.setBlue(2 * avg - 255); } } else if (y < height * 5 / 7) { //blue if (avg < 128) { pixel.setRed(0); pixel.setGreen(0); pixel.setBlue(2*avg); } else { pixel.setRed(2*avg-255); pixel.setGreen(2 * avg - 255); pixel.setBlue(255); } } else if (y < height * 6 / 7) { //indigo if (avg < 128) { pixel.setRed(.8*avg); pixel.setGreen(0); pixel.setBlue(2*avg); } else { pixel.setRed(1.2*avg-51); pixel.setGreen(2 * avg - 255); pixel.setBlue(255); } } else { //violet if (avg < 128) { pixel.setRed(1.6*avg); pixel.setGreen(0); pixel.setBlue(1.6*avg); } else { pixel.setRed(0.4*avg+153); pixel.setGreen(2 * avg - 255); pixel.setBlue(0.4*avg+153); } } } } var img = new SimpleImage("chapel.png"); filterRainbow(img); print (img);

### Available Images

chapel.png
[231x308]

rodger.png
[315x424]

astrachan.jpg
[240x360]

duvall.jpg
[200x300]

hilton.jpg
[140x210]

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.