1

Given this sample code that replaces all the blue with a new background:

public void chromakeyBlue(Picture newBg)
{
    Pixel [] pixelArray = this.getPixels();
    Pixel currPixel = null; 
    Pixel newPixel = null;

    for (int i = 0; i < pixelArray . length ; i++) { 
        currPixel = pixelArray [ i ];

        if (currPixel.getRed() + currPixel.getGreen() < currPixel . getBlue ())
        { 
            newPixel = newBg.getPixel(currPixel.getX(), 
                                      currPixel.getY ( ) ) ;
            currPixel . setColor ( newPixel . getColor ( ) ) ;
        } 
    }
}

I was wondering if using this condition :

currPixel.getRed() < currPixel.getBlue() && currPixel.getGreen() < 
currPixel.getBlue()

on the if statement, if it would work or not. In other words, does using that condition have the same affect as currPixel.getRed() + currPixel.getGreen() < currPixel . getBlue ()?

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
emily
  • 11
  • 3
  • That's not JavaScript. Did you mean Java? Anyway, I don't see how comparing blue to (red + green) could have the same result as separately comparing red to blue and green to blue, because, e.g., what if red and green are both 5 and blue is 8?. – nnnnnn Nov 03 '17 at 08:15

1 Answers1

1

currPixel.getRed() + currPixel.getGreen() < currPixel.getBlue() will test if blue is greater than the the sum of the red + green. So if RGB is 10,10,15 this condition evaluates to false.

currPixel.getRed() < currPixel.getBlue() && currPixel.getGreen() < currPixel.getBlue() will test if blue is greater than red and blue is greater than green. So if RGB is 10,10,15 this condition evaluates to true.

So no, both conditions do not have the same effect. (If visually they appear to be similar, that may just be coincidental test outcomes)

Lee Kowalkowski
  • 11,591
  • 3
  • 40
  • 46