Skip to content

Balthazar Auger

Using BitmapData.threshold()

1 min read

Since one of my objectives is to be able to publish this yet unnamed game to the web, one of my main concerns is keeping the total download size low, relying on as little static art as possible.

One cool thing I’ve always wanted to try out is palette swapping: basically you take the same bitmap and you replace color values to change the way it looks. Most widely known for its ability to recycle the same beat’em up enemy in several levels by changing pixel colors without requiring a whole new bitmap being stored on the ROM.

In my case, I use it for far more utilitarian goals: to signal player ownership of a given vehicle.

Thanks to openFl’s near perfect port of the flash API, BitmapData.threshold() is available. You give that method a source bitmap, a threshold color, a replacement color and an operation (==, <=, etc) and all pixels that match the condition will see their color set to the replacement color.

I added the color swap method right into my asset manager class:

1public function getColoredTank(p_color1:UInt, p_color2:UInt):BitmapData
2 {
3 var coloredTank:BitmapData = baseTank.clone();
5 //apply first color, replacing Magenta (0xFFFF00FF)
6 coloredTank.threshold(
7 coloredTank,
8 coloredTank.rect,
9 coloredTank.rect.topLeft,
10 "==", 0xFFFF00FF, p_color1);
12 //apply second color, replacing Yellow (0xFFFFFF00)
13 coloredTank.threshold(
14 coloredTank,
15 coloredTank.rect,
16 coloredTank.rect.topLeft,
17 "==", 0xFFFFFF00, p_color2);
19 return coloredTank;
20 }

This allows me to turn this:


Into these:

Workspace 1_004

Both tanks in the bottom image share the same source bitmap.

Since I’ve had some fun playing with this, I decided to see about allowing players to set their own color preferences for their vehicles, which might be a nice touch in multiplayer.

© 2020 by Balthazar Auger. All rights reserved.
Theme by LekoArts