#Color tolerance for replacing colors and performance improvements

1 messages · Page 1 of 1 (latest)

stoic aspen
#

I'm trying to replace colors in a texture. This works for flat colors, but it has to be an EXACT match. is there any way I can try and implement a sort of tolerance system, similar to how paint buckets work?

foreach (var cPP in ColorReplacementPairs)
        {
            for (var index = 0; index < colors.Length; index++)
            {
                var col = colors[index];
                if (col != cPP.ColorToReplace)
                {
                    continue;
                }
                colors[index] = cPP.ReplacementColor;
                // Debug.Log("COLOR HAS BEEN REPLACED!");
            }
        }
#

its also quite slow cus im literally going through every pixel

main solar
# stoic aspen I'm trying to replace colors in a texture. This works for flat colors, but it ha...

Of course there is a way to add tolerance, infinitely many ways in fact. One way would be to accept everything within some distance in the 3D space defined by RGB. Other color spaces like HSV (3D position within the HSV cylinder could be used) will give bit different results. There are also many ways you could go about replacing the color. Should everything within the acceptable range get the same replacement colors or should colors further away from the color to replace get only slight tint of the replacement color? If the latter, then there are also countless ways you could mix the colors and choose different fade functions over distance.

#

You would also need to take into account the edge cases where if you have colors to replace close to each other, some color could get within each of the tolerance thresholds, which color should it get?

main solar
# stoic aspen its also quite slow cus im literally going through every pixel

If you need to go through every pixel, then there's not much you can do. In this case you are also going through every color replacement pair per every pixel so that's something too. You could use dictionary to find matches in O(1) time, that wouldn't work with the tolerance stuff though (some kind of bucketing could still be used in case there are a lot of colors to replace). One of the only easy ways to speed things up would be to use more processor units, multithreading in other words. The absolutely fastest solution of course would be to utilize the GPU which is the ultimate multithreader but that also has it's limitations

stoic aspen
main solar
stoic aspen
#

i feel like im a bit in over my head with this thing at this point 😅

stoic aspen
main solar
stoic aspen
#

can i convert a to and from tex2ds and rendertexs?

main solar
main solar
# stoic aspen interesting. what docs should I look at for something like this?

At least the official documentations I could find don't seem particularly beginner friendly (or particularly useful in general even). You can look for Compute Shader tutorials, I'm sure you can find plenty in video and text forms. Compute Shaders can look bit daunting at first with all the different IDs and stuff, but conceptually they are really simple creatures

#

Basically you just tell the GPU to do X amount of calculations and then you can define in the shader code what those calculations should include (in your case: read pixel from texture -> replace if needed -> write back to texture)

stoic aspen
#

i'll have a check on this. could be a fun little detour

#

thank you!

main solar
#

Np, compute shaders can definitely be a useful tool to have on ones toolbox or at least be aware of their existence. A lot of graphics related stuff (and other repeated math heavy stuff) can be done really really quickly on the GPU

stoic aspen