Converting 8 bit color into RGB value

前端 未结 2 1484
长情又很酷
长情又很酷 2020-12-17 00:07

I\'m implementing global illumination in my game engine with \"reflective shadow maps\". RSM has i.a. color texture. To save memory. I\'m packing 24 bit value into 8 bit val

2条回答
  •  悲哀的现实
    2020-12-17 00:30

    To convert 8bit [0 - 255] value into 3bit [0, 7], the 0 is not a problem, but remember 255 should be converted to 7, so the formula should be Red3 = Red8 * 7 / 255.

    To convert 24bit color into 8bit,

    8bit Color = (Red * 7 / 255) << 5 + (Green * 7 / 255) << 2 + (Blue * 3 / 255)
    

    To reverse,

    Red   = (Color >> 5) * 255 / 7
    Green = ((Color >> 2) & 0x07) * 255 / 7
    Blue  = (Color & 0x03) * 255 / 3
    

提交回复
热议问题