Choose 15/16 bit Colour

If one uses the the previous "Load and Convert Bitmap Colours" routine there is a high possibility that a colour chosen on a 32/24 bit screen will not display correctly on a 15 or 16 bit screen.

The following routine shows what red, green and blue values to use so that when displaying on either a 15 or 16 bit screen the colour will be converted correctly.

/*
The RGB888 pixel data
    a red pixel   (0000 0000 1111 1111 0000 0000 0000 0000)
    a green pixel (0000 0000 0000 0000 1111 1111 0000 0000)
    a blue pixel  (0000 0000 0000 0000 0000 0000 1111 1111)

The RGB555 pixel data:
    a red pixel   (0111 1100 0000 0000)
    a green pixel (0000 0011 1110 0000)
    a blue pixel  (0000 0000 0001 1111)

The RGB565 pixel data:
    a red pixel   (1111 1000 0000 0000)
    a green pixel (0000 0111 1110 0000)
    a blue pixel  (0000 0000 0001 1111)

16bpp RGB555 masks 0x7C00 0x3E0 0x1F
    r1 = (c & 0x7C00) >> 7;
    g1 = (c & 0x03E0) >> 2;
    b1 = (c & 0x001F) << 3;

16bpp RGB565 masks 0xF800 0x7E0 0x1F
    r1 = ((c & 0xF800) >> 8);
    g1 = ((c & 0x07E0) >> 3);
    b1 = ((c & 0x001F) << 3);

32bpp RGB888 masks 0xFF0000 0xFF00 0xFF
    r1 = ((BYTE)(c));
    g1 = ((BYTE)(((WORD)(c)) >> 8));
    b1 = ((BYTE)((c) >> 16));
*/

#include <windows.h>
#include <stdio.h>

#define MakeRGB555(r, g, b) ((WORD)(((BYTE)(r) >> 3) << 10) \
                            | (((BYTE)(g) >> 3) << 5) | ((BYTE)(b) >> 3))
#define MakeRGB565(r, g, b) ((WORD)(((BYTE)(r) >> 3) << 11) \
                            | (((BYTE)(g) >> 2) << 5) | ((BYTE)(b) >> 3))

int main(void)
{
    int c;
    int r = 133, g = 41, b = 143; // the required colour
    int r1, g1, b1;

    printf("Original colour -> %d %d %d\n", r, g, b);

    // make 15 bit colour from r,g,b
    c = MakeRGB555(r, g, b);
    // extract the r/g/b components
    r1 = (c & 0x7C00) >> 7;
    g1 = (c & 0x03E0) >> 2;
    b1 = (c & 0x001F) << 3;
    printf("Possible RGB555 colour -> %d %d %d\n", r1, g1, b1);

    // make 16 bit colour from r,g,b
    c = MakeRGB565(r, g, b);
    // extract the r/g/b components
    r1 = ((c & 0xF800) >> 8);
    g1 = ((c & 0x07E0) >> 3);
    b1 = ((c & 0x001F) << 3);
    printf("Possible RGB565 colour -> %d %d %d\n", r1, g1, b1);

    // make 32/24 bit colour from r,g,b
    c = RGB(r, g, b);
    // extract the r/g/b components
    r1 = ((BYTE)(c));
    g1 = ((BYTE)(((WORD)(c)) >> 8));
    b1 = ((BYTE)((c) >> 16));
    printf("The RGB888 colour -> %d %d %d\n", r1, g1, b1);

    return 0;

}

Back to main page