Sprite Sheet pink color as alpha ?

Hi Everyone. I was watching Notch’s coding ( Prelude of the chambered ), and I saw that in his spritesheet there is pink color, but he’s using it as alpha.

How does he do that ???

It isn’t necessary actually. What graphics program do you use to create sprites? I recommend paint.net. Just delete the White layer and create a transparent background.

I know that, but I’m just curious.

Well, since the game loads the images as pixel-array, he can just check if the pixel’s color is pink and ignore it:

	public static Bitmap loadBitmap(String fileName) {
			[...]
			for (int i = 0; i < result.pixels.length; i++) {
				int in = result.pixels[i];
				int col = (in & 0xf) >> 2;
				if (in == 0xffff00ff) col = -1;
				result.pixels[i] = col;
			}
			[...]
	}

[quote]if (in == 0xffff00ff) col = -1;
[/quote]
If the color is pink, it will be set as -1 which java counts as transparent

Thanks !

“(in & 0xf) >> 2” what’s the point of this? You are only keep the rightmost 4 bits if they are 1, then shifting them…?

-1 is fully opaque and fully white on the ARGB scale…

Because if he doesn’t mask first, he’ll shift in non-zero bits.

It doesn’t make much sense to me either.

It takes the first 4 least significant bits (rightmost) and then shifts them to the right by 2 bits (effectively erasing the first 2 bits) so that you end up with the third and fourth bits from the Blue color at the beginning of the int. The resulting data that is saved into the variable “col” will be ‘000000000000000000000000000000xx’ where ‘xx’ is the third and fourth bits from the blue color. The following if statement “in == 0xffff00ff)” will always return false.

Actually, you always shift in bits of zero.

<<      Signed left shift // Always shifts in 0's from the right.
>>      Signed right shift // Shifts in copies of the MSB (left most bit) from the left.
>>>     Unsigned right shift // Always shifts in 0's from the left.

Just check the resulting pixel for the pink color. And if you’ve got Alpha enabled (most likely) make the pixel completely transparent (Set leftmost 8 bits to 0). Or if you’re not using Alpha you need to omit drawing the specified pixel on the screen altogether.


int col = result.pixels[i];
if (col == Color.pink.getRGB()) col = 0; // Sets all the bits to 0, including the Alpha in ARGB.
result.pixels[i] = col;

Some more random bit manipulation posts.


Pink/Megenta ist btw classically used to mark transparent parts.

Since its a “spare” color, not usually not used in Artwork, and easy to recognize.
(full red and blue, 0 green channel)

You can use any other spare color too for that purpose, if magenta looks
too ugly when editing the spritesheet.

Almost always it is better to just save the PNG/GIF with transparency…

Well, this comes from the times, when images (like BMPs) where used
without alpha channel, or when there was just a limited colorpalette, to save size and have better compression.

Not that long ago actually, (100kB heap memory JavaME games etc…)

When you’re using images with a palette based color format, then one color is dedicated to full transparency. It’s sort of like green screen for sprites. By convention, it’s pink (#FFFF00) to avoid conflicts with color is more likely to be included in the palette. It could also have been black, grey, white, bright green, etc. but that colors probably the least likely to appear in most sprites. GIMP and other image editors that can handle alpha channels show a background pattern instead of a solid background replacement color.