# [MDX] Replacing specific colors in an image on the fly

This topic is 4483 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I recently acquired the files from a particular old game which I loved. I now can't wait to start working on a game with it. However, the problems I currently have with the original data files: All the units, buildings, etc. are in 1 specific color: Goldish. Now, I wanna use C# and DirectX 9 in 2D mode for this game, but I don't feel like replacing the colors in those images just for the sake of being able to change the side they belong too. What would be my best way to do this, specifically on the fly(So I can keep memory usage down). Toolmaker [Edited by - Toolmaker on April 7, 2006 6:19:27 PM]

##### Share on other sites
Well you could easily use a pixel shader to do precise and "soft" colour replacement.

However, if you don't want that then an old trick I used worked through the traditional fixed-function texture blending. I was using it for 2D sprites where I wanted red/blue highlights on units in an RTS so I could identify what team they were on...

At load time (or pre-process) you can create a texture where white=replaced and black=ignored (sort of like an alpha map). You can then render the original artwork with (1-map) and then in a second pass rendering the coloured artwork (multiplied by a TFACTOR to get the desired result) scaled by the 'map' value. I forget the exact combination, but you can usually do this in a single pass by configuring the appropriate states.

hth
Jack

##### Share on other sites
I haven't done much with DirectX, so if you could show me SOME example code(Would be nice if it would be in C#), or give me a more thorough explanation.

I have this pic:

I want to replace all the yellowish/goldish colors with different colors. The image itself needs to be replaced with smaller, easier to process images.

The idea is that I can make this game multiplayer, and player can choose from a wide variety of colors for their side.

Toolmaker

##### Share on other sites
"Dune 2 : Battle for Arrakis" if I'm not mistaken? [smile]

Spent months playing that on my old Amiga 1200, absolutely loved it. Although I seem to remember that I could never complete the final level though [headshake]

The problem with your image is that the sand is a similar orange/gold colour to the parts that you want to use to identify teams. If you do the colour replacement as you render the individual tiles then it shouldn't be so much of a problem.

I don't have any C# code as I'm not that way inclined [lol] One of the other regulars may well be able to help you out in this case.

What sort of hardware/configuration are you targetting? Will you be able to use a pixel shader? and, to be honest, even duplicating that image 8 ways in memory at load-time is unlikely to cause you that much of a storage problem on any computer from this century [smile]

If you take my idea of an "alpha map" type approach, it might be best to manually create this yourself and ship it alongside. Given the colours in the image you presented, it could be quite easy to create an algorithm that generates a lot of "false positives" and replace the wrong colours. Would look a little odd if bits of sand started turning red/blue/green [lol]

Cheers,
Jack

##### Share on other sites
Surely those Dune 2 graphics are still protected by EA's copyright. I doubt one could legally ship anything derived from them without a license.

##### Share on other sites
I hadn't thought of that - the game must be 10 years old by now?

Quote:
 Original post by MastabaSurely those Dune 2 graphics are still protected by EA's copyright. I doubt one could legally ship anything derived from them without a license.
It is indeed a legal minefield [oh]

Best be sure what you can (and can't) do with these assets before you invest too much effort...

Cheers,
Jack

##### Share on other sites
Quote:
Original post by jollyjeffers
I hadn't thought of that - the game must be 10 years old by now?

Quote:
 Original post by MastabaSurely those Dune 2 graphics are still protected by EA's copyright. I doubt one could legally ship anything derived from them without a license.
It is indeed a legal minefield [oh]

Best be sure what you can (and can't) do with these assets before you invest too much effort...

Cheers,
Jack

Don't worry, I won't distribute anything with those graphics ;). I plan on using the original Dune II data files for it. But, that still requires that I replace some of the colors. And the game is 15 years old by now ^_^.

Since I got the extracted files aswell(I do own a copy of the game), I can first attempt to use this technique with the extracted files. This makes it easier to see if the results are ok.

Toolmaker

##### Share on other sites
Quote:
 I don't have any C# code as I'm not that way inclined

No worries, Jack. Human flaws are charming, I'm told [wink]

Be that as it may, below is a little alpha map I made for this texture. You seem to be missing the tanks, so I guess you're going to need some more of these made. Send me a PM and I'll fix em up for you (this is easily done automatically by loading a new pallette for the image).

To actually make something of this in a game, I'd go with Jack's advice and use a pixel shader for rendering the textures. In this pixel shader you can use the normal texture for the basic rendering and blend the selected player color on it using the alpha map, basically like this:

float mask = tex2D(alphaMap, texCoord0);OUT.color.rgb = (1 - mask) * tex2D(texture, texCoord0) + mask * selectedColor;OUT.color.a = 1;

I'm not entirely sure about using the rgb swizzle here, but you will need to blend the selected unit color onto the original texture at full opacity, since you want to overwrite the color value in the original texture. If you don't blend at full opacity, goldish color will show through, which is probably not what you want.

I'll try to fix up a little demo sample tomorrow, the prospect of Dune2 in MDX deserves that [smile]

##### Share on other sites
The tiles are made using indexed color.

Unless the palette was changed by photoshop, then indices 192-198 are used to display units. It doesn't matter what color is stored in the palette, when rendering the tile, just replace indices in that range with corresponding side-designating color.

The color index 223 is for palette color cycle effect (used to display pulsation of wind traps).

Key is to take into account the index, not the color.

Since the indices used for blending are c0-c6, you could probably calculate the alpha value directly from (index & 0xf) or something similar.

192 is brightest, 198 is darkest, the distribution between them looks uniform.

Oh and, Dune did this on the fly, so I guess today's terraflop GPUs should be able to do that as well.

##### Share on other sites
Ok, I've whipped up a something simple with MDX, and loaded a harvester(Why always the harvesters? [grin]).

Basicly, I have 2 problems at this point:
The colors of the vehicles, and ofcourse, the 'background' of all the images.

So, from what I understood, I can do this with alpha-blending the right color. For that, I need to store the images in a format that supports alpha-blending. Sounds fair to me at this moment.

What would be the best way to alter my pics? I think PNG would suffice as format, and MDX can load those perfect. What tool do I need for this, and in what way do I need to "alter" the images, so the sides of vehicles are more darker than the rest?

Yes, I could ask Remigius to do it, but that would require him to edit quite a few pics. Because, I want to stick with smaller images, per building/vehicle. So, ideas?

Toolmaker

1. 1
Rutin
25
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 9
• 9
• 46
• 41
• 23
• ### Forum Statistics

• Total Topics
631750
• Total Posts
3002052
×