Team colors - how are they done?

Discussion of all aspects of the game engine, including development of new and existing features.

Moderator: Forum Moderators

Post Reply
kazi
Posts: 2
Joined: July 6th, 2015, 12:41 am

Team colors - how are they done?

Post by kazi »

Hey all, I am developing a Wesnoth-ish game for Android at the moment in Java (as a for-fun project/to get better at coding). Although I'm reasonably far along, I had a couple of questions on how Battle for Wesnoth performs certain operations:

How are team colors calculated and applied from unit .png files? Right now, I see that all Wesnoth units have pink regions corresponding to the area where team colors will go. How are those pink regions detected and recolored? If you're not entirely sure, would it be possible to point me to the relevant section of the source code?

Note: I'm not interested in copying Wesnoth assets or code, I simply want to understand how things work so I can implement a similar system in my own game engine.

Thanks! :eng:
User avatar
Iris
Site Administrator
Posts: 6798
Joined: November 14th, 2006, 5:54 pm
Location: Chile
Contact:

Re: Team colors - how are they done?

Post by Iris »

kazi wrote:How are team colors calculated and applied from unit .png files? Right now, I see that all Wesnoth units have pink regions corresponding to the area where team colors will go. How are those pink regions detected and recolored? If you're not entirely sure, would it be possible to point me to the relevant section of the source code?
The WML API for this is pretty simple. Unit definitions include a name reference to the team color palette to use as the TC source. (For mainline units, this is always the magenta palette; flags use the flag_green palette instead.) The game assigns a team color range to each side in a scenario.

The mainline color ranges and color palettes are defined in data/core/team-colors.cfg. Palettes are pretty simple comma-separated lists of (exact) colors in hex RRGGBB notation, and color ranges are quartets of color values in the same format but with a more specific meaning, described near the start of the file:

Code: Select all

# Each Team-color definition is made up of 4 hexadecimal colors,
# one byte each of red-green-blue information
#  1) the average shade of a unit's team-color portions
#  2) the maximum highlight shade of a unit's team-color portions
#  3) the minimum shadow shade of a unit's team-color portions
#  4) the color of the markers on the mini-map.
For performing the actual recoloring, the game calculates a replacement palette by applying the required color range (say, the range with id 1, which is equivalent to red and used for side 1) on the source palette using the recolor_range() function (warning: maths inside); then takes the target image and performs a pixel-by-pixel substitution using the color mappings returned by the function.

Hope this helps!
Author of the unofficial UtBS sequels Invasion from the Unknown and After the Storm.
kazi
Posts: 2
Joined: July 6th, 2015, 12:41 am

Re: Team colors - how are they done?

Post by kazi »

Excellent, that's exactly what I was looking for. I'll give things a try and see what happens. (Implementation may be a bit tough, but at least it gives me a concept to start with)

Thanks for your help!
Post Reply