how to color mask in c

后端 未结 4 910
自闭症患者
自闭症患者 2021-01-26 04:32

how do you color mask a 32 bit unsigned integer for the red, green, and blue values

is it like this? (color_to_be_masked>>8)

4条回答
  •  抹茶落季
    2021-01-26 05:22

    "It depends", namely on which bits are which color.

    Often they're mapped "backwards", so that Red is in the lower-most bits, green in the "middle", and blue on top (sometimes followed by alpha, if that is being used).

    Assuming 8 bits per component, you would have:

    uint32_t abgr = 0x80eeccaa;  /* Or whatever. */
    const uint8_t red = abgr & 0xff;
    const uint8_t green = (abgr >> 8) & 0xff;
    const uint8_t blue = (abgr >> 16) & 0xff;
    const uint8_t alpha = (abgr >> 24) & 0xff;
    

    If you're really using "rgba" component order, swap the above around:

    uint32_t rgba = 0xaaccee80;  /* Or whatever. */
    const uint8_t red = (abgr >> 24) & 0xff;
    const uint8_t green = (abgr >> 16) & 0xff;
    const uint8_t blue = (abgr >> 8) & 0xff;
    const uint8_t alpha = abgr & 0xff;
    

    Note that I shift before I mask, that's nice since it makes the constant that forms the mask smaller which is potentially more efficient.

提交回复
热议问题