When converting between low-bit color color formats (R4G4B4A4, R5G6B6, R8G8B8A8, and so on), the conversion routines used often don't map to the nearest color channel values in the target bit depth. For example, when converting 8bit to 4bit colors, this is typically done by simply the discarding the lower 4 bits of each channel. This is fast, but not very accurate; the maximum error of this method is 15 units in the source range compared to an error of 8 units when mapping to the closest value.

Inspired by this stackoverflow answer, which lists a method of expanding 5 and 6 bit color channels to their closest equivalent 8bit value with a cheap combination of integer multiplication, addition and shifting, the following table lists conversion formulas for mapping between UNORM ranges of 4, 5, 6, 8, 10 and 11 bits with correct rounding. This is enough to cover all common color formats (4444, 555, 565, 8888, 10-10-10, 11-11-10) and build fast and accurate conversion routines for them.

4bit Target | 5bit Target | 6bit Target | 8bit Target | 10bit Target | 11bit Target | |
---|---|---|---|---|---|---|

4bit Source | x | (x * 33 + 8) >> 4 | (x * 67 + 9) >> 4 | x * 17 | (x * 1091 + 9) >> 4 | (x * 546 + 1) >> 2 |

5bit Source | (x * 2 + 1) >> 2 | x | (x * 65 + 16) >> 5 | (x * 527 + 23) >> 6 | x * 33 | (x * 2113 + 16) >> 5 |

6bit Source | (x * 61 + 128) >> 8 | (x * 2 + 1) >> 2 | x | (x * 259 + 33) >> 6 | (x * 4157 + 128) >> 8 | (x * 130 + 1) >> 2 |

8bit Source | (x * 15 + 135) >> 8 | (x * 249 + 1024) >> 11 | (x * 253 + 512) >> 10 | x | (x * 1027 + 129) >> 8 | (x * 65761 + 4096) >> 13 |

10bit Source | (x * 961 + 32768) >> 16 | (x * 31 + 527) >> 10 | (x * 1009 + 8192) >> 14 | (x * 1021 + 2048) >> 12 | x | (x * 2049 + 512) >> 10 |

11bit Source | (x * 1921 + 131072) >> 18 | (x * 993 + 32256) >> 16 | (x * 2017 + 32768) >> 16 | (x * 2041 + 8192) >> 14 | (x * 2 + 1) >> 2 | x |

Each formula yields the exact same result as truncate(float(x) / float((1 << sourceBits) - 1) * float((1 << targetBits) - 1) + 0.5f). You can also find the full table for up to 16 bits here.