# help: arithmetic on conversion between different color detph

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

## Recommended Posts

is anyone help me? thanks

##### Share on other sites
Color depths come in a lot of varieties. You've got 8-bit indexed (we'll ignore this one), r5g6b5, r5g5b5a1, a8r8g8b8, x8r8g8b8, etc. etc. The only ones where you have to do actual conversion instead of just rearranging the bytes is going from 24- or 32-bit to 16-bit.

To convert from 8-bit to lower bits, shift right:
char r = 128; //half intensity, 8 bits.
r >> 3; //r == 16. Half intensity, 5 bits.
//OR
r >> 2; //r == 32. Half intensity, 6 bits.

~BenDilts( void );

##### Share on other sites
Generally, converting from a color depth of m bits to n bits is done like this:
    Cn = Cm * ( 2n-1 ) / ( 2m-1 )
However, since you are usually dealing with integers, you must also include rounding:
    Cn = ( Cm * ( 2n-1 ) + ( 2m-1 ) / 2 ) / ( 2m-1 )
For example, to convert from 8 bits to 4 bits:
    C4 = ( C8 * 15 + 127 ) / 255
That gives you the following conversions:
      0    0      8    0     16    1     32    2     64    4    128    8    191   11    219   13    239   14    247   15    255   15
Note: simply shifting as BeanDog suggests will give you poor results (though they may be adequate).

i get it,

thanks you all.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 23
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
634779
• Total Posts
3019240
×