SDL_SetPalette(3)
π NAME π
SDL_SetPalette β sets the colors in the palette of an 8-bit surface. that's the whole job bestie, and she does it flawlessly π¨β¨
πΈ SYNOPSIS πΈ
#include "SDL.h"
int SDL_SetPalette(SDL_Surface *surface, int flags, SDL_Color *colors,
int firstcolor, int ncolors);
π¦ DESCRIPTION π¦
okay so. SDL_SetPalette sets a portion of the palette for the given 8-bit surface. not necessarily the whole thing β a slice, a lil palette snack, a vibe π
palettized (8-bit) screen surfaces that have the SDL_HWPALETTE flag are lowkey living a double life: they have two palettes. there's the logical palette, which is what gets used for mapping blits to/from the surface, and then there's the physical palette, which is what decides how the hardware maps the colors to the display. and SDL_BlitSurface? she always uses the logical palette when blitting surfaces (that's only when it has to convert between surface pixel formats, but still). because of this whole situation, it's often useful to modify only one or the other palette to get various special color effects β think screen fading, color flashes, screen dimming. the special effects serve, periodt π
this function can modify either the logical or the physical palette by specifying SDL_LOGPAL or SDL_PHYSPAL in the flags parameter. pick your fighter π₯
when surface is the surface associated with the current display, the display colormap will be updated with the requested colors. and if SDL_HWPALETTE was set in the SDL_SetVideoMode flags, SDL_SetPalette will always return 1, and the palette is guaranteed to be set the way you desire β even if the window colormap has to be warped or run under emulation. iconic behavior honestly, no notes π
the color components of a SDL_Color structure are 8-bits in size, which gives you a total of 256^3=16777216 colors. that's sixteen million plus, bestie, go wild π
π RETURN VALUE π
- if surface is not a palettized surface, this function does nothing and returns
0. it said "not my problem" π - if all of the colors were set exactly as passed to
SDL_SetPalette, it returns1. slay β¨ - if not all the color entries were set exactly as given, it returns
0β and in that case you should go look at the surface palette to figure out what the actual color palette ended up being. detective mode activated π
π EXAMPLE π
/* Create a display surface with a grayscale palette */
SDL_Surface *screen;
SDL_Color colors[256];
int i;
.
.
.
/* Fill colors with color information */
for(i=0;i<256;i++){
colors[i].r=i;
colors[i].g=i;
colors[i].b=i;
}
/* Create display */
screen=SDL_SetVideoMode(640, 480, 8, SDL_HWPALETTE);
if(!screen){
printf("Couldn't set video mode: %s
", SDL_GetError());
exit(-1);
}
/* Set palette */
SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, 256);
.
.
.
.
notice how she hits bothSDL_LOGPALandSDL_PHYSPALwith that|? that's the whole point, bestie β logical AND physical, together, holding hands π«Ά
π SEE ALSO π
SDL_SetColorsSDL_SetVideoModeSDL_SurfaceSDL_Color
and that's the tea, bestie π΅π this whole lil guide came straight out of the SDL API Reference (that's the manual name, section 3, iykyk) β source is SDL, and it's dated Tue 11 Sep 2001, 23:01. a genuine vintage icon, we love her, mwah πππ§
π¦ where this page comes from
- debian bookworm: libsdl1.2-dev 1.2.15+dfsg2-8 Β· LGPL-2.1-or-later
- nixos 26.05: SDL_sixel 0-unstable-2016-02-06
- nixpkgs unstable: SDL_sixel 0-unstable-2016-02-06
- ubuntu jammy: libsdl1.2-dev 1.2.15+dfsg2-6 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms