How to convert 24 bit RGB to 8 bit RGB

后端 未结 3 1843
离开以前
离开以前 2021-01-05 18:23

I was wondering what is the best way to convert a 24-bit RGB color (8 bits per color) into an 8-bit color (2bits Blue, 3 Green, 3 Red). I\'d like C code for doing this.

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-05 19:12

    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 (RRRGGGBB),

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

提交回复
热议问题