What is the correct way to select color depth?

Hi everyone,

I’m new to the world of SDL, and I’m trying to run my SDL program with a 16-bit color depth. For this purpose, I’ve chosen the following APIs:

*(1) SDL_Surface icon = SDL_CreateRGBSurfaceWithFormat(0, width, height, 16, SDL_PIXELFORMAT_RGB565);

(2) //Extracting RGB from Color
R=((Color >> 11) & 0x1F);
G=((Color >> 5) & 0x003F);
B=(Color & 0x001F);

SDL_SetRenderDrawColor(renderer,R,G,B, 0);  

and geeting following output when run:


Now I’m confused. What am I doing wrong here? Are there any other sets of APIs available for color depth? Please help.

Shrikant Vaishnav

If you want the color components in the range 0-255 you can use SDL_GetRGB.

Uint8 R, G, B;
SDL_GetRGB(Color, icon->format, &R, &G, &B);

This will set a transparent draw color, which is probably not what you want.

If you’re trying to set a transparent background for your icon, you can do something like this:
SDL_SetColorKey(icon, SDL_TRUE, *(Uint16 *)icon->pixels);

@Peter87 Sir,means instead of these line:
R=((Color >> 11) & 0x1F);
G=((Color >> 5) & 0x003F);
B=(Color & 0x001F);

I should use this:

SDL_GetRGB(Color, icon->format, &R, &G, &B);

@slouken Sir I’m actually trying to create a simulator for any graphic LCD using its Pixelset function. Could you guide me for this purpose which SDL function I should use ? I’m new to SDL and am only familiar with foreground and background concepts…what is transparent background and transparent draw concept?

Yes, you can replace those lines with what I wrote. Just make sure R, G and B are of type Uint8 (or uint8_t) otherwise the code won’t compile.