Beware Copy-Paste Bugs

I tried to make some images fade in and out. They had to fade in slowly, stay there a while, and then fade out slowly as an alternative to an image slideshow. (The original slideshow was way too jerky in our new higher resolution.) I went through several implementations and even tried some alternate effects. Some of them worked, but none of them looked quite right.

I finally made a fade in/out effect that worked (after some debugging) except for one bizarre detail: it always produced grayscale images.

I checked through the code in the class to see if I was getting my color components mixed up. I was not. Or so it seemed.

I was about to post here about it, but then I decided to check a couple of simple methods that I was using. I had been using them for a long time, so they should have been fully debugged and ok. But they weren’t.

With method comments removed, here’s the buggy code:

public static int getBlueComponent(final int color) {
	return (color & 0x00ff0000) >> 16;
} //end getBlueComponent

public static int getGreenComponent(final int color) {
	return (color & 0x00ff0000) >> 16;
} //end getGreenComponent

public static int getRedComponent(final int color) {
	return (color & 0x00ff0000) >> 16;
} //end getRedComponent

You can see that the green and blue methods are copies of the red method. Apparently, I forgot to change the code when I copy-pasted the code at some point. I know that I was moving code around and changing comments a while ago (my build log indicates that I did this on November 2). I must have screwed it up then.

Here’s the corrected code with method comments removed:

public static int getBlueComponent(final int color) {
	return (color & 0x000000ff);
} //end getBlueComponent

public static int getGreenComponent(final int color) {
	return (color & 0x0000ff00) >> 8;
} //end getGreenComponent

public static int getRedComponent(final int color) {
	return (color & 0x00ff0000) >> 16;
} //end getRedComponent

I implemented the same code twice and tried a couple of alternative effects (which worked but didn’t look quite right) because of that little bug. :frowning:

BEWARE!!! :wink:

Check, check, double check, write that unit-test, check again