SDL_SetColors(3)
π SDL_SetColors β bestie edition π
okay so you're in the SDL API Reference and you found SDL_SetColors(3)?? slay, come sit with me, this one's actually so fun once it clicks π«Ά
β¨ NAME β¨
SDL_SetColors β sets a portion of the colormap for the given 8-bit surface. that's it, that's the whole job, and it eats every time π
πΈ SYNOPSIS πΈ
#include "SDL.h"
int SDL_SetColors(SDL_Surface *surface, SDL_Color *colors, int
firstcolor, int ncolors);
yes bestie, four args, don't panic, we're going through every single one π
π¦ DESCRIPTION π¦
So. SDL_SetColors sets a portion of the colormap for the given 8-bit surface. a portion, bestie, not the whole thing β you get to pick where it starts and how far it goes, main character energy fr fr β¨
When surface is the surface associated with the current display, the display colormap will be updated with the colors you requested. and if SDL_HWPALETTE was set in the SDL_SetVideoMode flags?? then SDL_SetColors 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. no cap. it's giving "your palette, your rules" π
The color components of a SDL_Color structure are 8-bits in size, which gives you a total of 256^3 = 16777216 colors. seventeen million bestie!! that's so many fits to choose from π
Now here's the part that lowkey changes your whole life. Palettized (8-bit) screen surfaces with the SDL_HWPALETTE flag have two palettes:
- a logical palette β used for mapping blits to/from the surface
- a physical palette β determines how the hardware will map the colors to the display
SDL_SetColors modifies both palettes (if present). it is equivalent to calling SDL_SetPalette with the flags set to (SDL_LOGPAL | SDL_PHYSPAL). so like, you could do it the long way, but why would you π
π RETURN VALUE π
okay bestie this part matters, pay attention:
- If
surfaceis not a palettized surface, this function does nothing, returning0. it's giving "read the room, wrong surface" π - If all of the colors were set as passed to
SDL_SetColors, it will return1. slay!! β - If not all the color entries were set exactly as given, it will return
0β and you should look at the surface palette to determine the actual color palette. i.e. go check what actually happened, don't just assume π«
not me getting a 0 back and being like "but I asked nicely" β bestie, the hardware said no. check the surface palette. periodt.
π EXAMPLE π
here's a whole grayscale palette setup, iconic behavior:
/* 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_SetColors(screen, colors, 0, 256);
.
.
.
.
look at her go π§ all 256 entries, r, g, b all marching in lockstep β that's a grayscale gradient and she's serving π
π₯ SEE ALSO π₯
SDL_ColorSDL_SurfaceSDL_SetPaletteSDL_SetVideoMode
go read these next, iykyk π
okay that's the whole page!! this was written for the SDL manual, dated Tue 11 Sep 2001, 23:01 β yes bestie, 2001, she's vintage and she still hits π stay palettized, love u, bye!! πβ¨π¦
π¦ 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