Color masks


Could some please give a short description of the term ‘color mask’ (like used
in the 5.-8. param to ‘SDL_CreateRGBSurface’) ?

H?kon Skjelten

The color mask is a bitmask that determines what portion of a pixel is a
particular color component (red, green, or blue), and you also need to
specify how much to shift to the right a color before that routine can get
that particular RED, GREEN, or BLUE component. This varies based on the byte
order (big/little endian) of the system or the bits-per-pixel (typically
16-bit, 24-bit, or 32-bit)

In the source code, check out the testgl.c program and testalpha programs to
see how they are constructed.

Typically there is 8-bits for each color in 32-bit mode plus 8-bits for
In 16-bit, there is R.G.B = 5.6.5 or 5.5.5 where 5-bits for red, (5 or 6)
bits for green, 5 bits for blue. If its 5.5.5, the other extra bit is
sometimes used for alpha.

A typical 16-bit color might look like (5.5.5) – see below. The leftmost
bit is alpha.
This however might be different depending on the byte order of the computer
system. Therefore check the example programs to see what I mean

$0 00000 00000 00000 (binary)

In this color above if you wanted to get the GRN component the bitmask would
look like $0 00000 11111 00000 or 0x03E0 . Then the shift amount would also
be 5. After its shifted in the routine a bitwise AND is performed to get the
GRN component, for example.

blue_component = color >> 0 & blue_mask
grn_component = color >> 5 & grn_mask
red_component = color >> 10 & red_mask

You can use calc.exe to convert binary into hex, etc. I might be a bit off
in my description, but you get the general idea (hopefully). It helps to
understand a little bit about hexadecimal, binary, and bitmasks in general,
which are underrated.



Could some please give a short description of the term ‘color mask’ (like
used> in the 5.-8. param to ‘SDL_CreateRGBSurface’) ?

H?kon Skjelten