Red Filter

Solve It

There are many ways you could create a filter with a red hue. One way is to consider the overall brightness of a pixel and map it to the same brightness on a red-hue scale. This red scale has white and black at 100% brightness and 0% brightness respectively, but in the middle, where the grayscale color would be rgb(128,128,128), this scale has a red color rgb(255,0,0). You can see that the red (R) value increases with brightness up to 50%, then it stays 255. Green (G) and blue (B) values are zero up to 50% and then increase to 255 at 100%.

Code It

function filterRed(image) { for (var pixel of image.values()) { var avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue()) / 3; 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); } } } var img = new SimpleImage("chapel.png"); filterRed(img); print (img);

See It


Available Images







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.