Unwanted transparency in 16-bit color

BlitzPlus Forums/BlitzPlus Programming/Unwanted transparency in 16-bit color

DrMartin(Posted 2003) [#1]
Hi all

When I run my (windowed) game on a computer with 16-bit color some black things get transparent. I've picked 255,0,255 as transparent color on the graphics in question and it works fine in 24 or 32 bit. Any ideas?


WolRon(Posted 2003) [#2]
16 bit color only allows 5 bits per color. 5 bits only allows 32 shades per color. So your min and max colors would be 0, 0, 0 (black) and 31, 31, 31 (white).

My guess would be that 31, 0, 31 doesn't equal 255, 0, 255 because its just not the same value (31 is not 255) OR the shade that 31 represents is not exactly equivalent to the shade that 255 represents (could be 255, 254, 253, 252, 251, 250, 249, or 248).

Why black areas (0, 0, 0) would become transparent if the mask color is set to 255, 0, 255 is beyond me.

Whats the work around? I don't know. You could prevent the user from running your application in 16-bit color though.


Andy_A(Posted 2003) [#3]
Adding to what WolRon mentioned, scaling 31 to 255 would most likely be 248 (8 x 31 = 248). The reasoning is that there are 3 bits difference and the largest number different values represented by 3 bits is 8. So, multiplying by 8 would scale rgb values from 5 bit to 8 bit, dividing by 8 would scale rgb values from 8 bit to 5 bit. The best way to do this is to SHL 3 for 5 to 8 bit conversion and SHR 3 for 8 to 5 bit conversion.

My guess as to why black becomes transparent is that the routine that evaluates the rgb values passed to the masking function returns a zero when the rgb evaluation fails. So when you specify 255 for red and blue color values instead of 248, it returns a zero instead of 255 which outside the range of numbers represented by 5 bits, hence black.

If all of the above is correct, just make sure that the rgb values for 24/32 bit colors are even multiples of 8.

Of course the '5-6-5' 16 bit color scheme is different, the scaling value for green is 4 instead of 8, but should still work since all multiples of 8 are also multiples of 4. You are just omitting some of the range of valid values available for green.