Question

public class BlendablePicture extends Picture {
    public BlendablePicture(String filename) {
        super(filename);
    }

    public void blendRectWithWhite(int xMin, int yMin, int xMax, int yMax,
            double a) {
        int x;
        x = xMin;
        while (x <= xMax) {
            int y;
            y = yMin;
            while (y <= yMax) {
                Pixel refPix = this.getPixel(x, y);
                refPix.setRed((int) Math.round(refPix.getRed() * (1.0 + a)));
                refPix.setGreen((int) Math.round(refPix.getGreen() * (1.0 + a)));
                refPix.setBlue((int) Math.round(refPix.getBlue() * (1.0 + a)));

                y = y + 1;
            }
        }
    }
}

I need to blend the color white with the pixels but instead this code is just making everthing brighter! It needs to look like this:

Blended White - Illustrated

any help with this code would be appreciated!

Was it helpful?

Solution

Instead of

refPix.setRed ( (int) Math.round (refPix.getRed () * (1.0+ a) ));

Try something like

refPix.setRed ( (int) Math.round (refPix.getRed()*(1.0-a)+255*a ));

when a = 1.0, you get R*0.0+255*1.0 = 255

when a = 0.0, you get R*1.0+255*0.0 = R

when a = 0.5, you get R*0.5+255*0.5 (half half)

This works with any colour not just white, you just need to replace the 255 for red, green and blue with the colours of what colour you want to blend with it and you get RGB-averaged blending.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top