SDL_GetRGB(3)

πŸ’– SDL_GetRGB β€” get RGB values from a pixel in the specified format πŸ’–

okay bestie, welcome to the SDL3 FUNCTIONS corner of the manual, page SDL_GetRGB(3). this one is short, sweet, and honestly so useful. let's get into it ✨


✨ NAME ✨

SDL_GetRGB β€” pull the red, green and blue values out of a pixel, in whatever format that pixel happens to be in. yes, even the weird ones. πŸŽ€


🌸 SYNOPSIS 🌸

#include <SDL3/SDL_pixels.h>

void SDL_GetRGB(Uint32 pixelvalue, const SDL_PixelFormatDetails *format, const SDL_Palette *palette, Uint8 *r, Uint8 *g, Uint8 *b);

that's the whole vibe. five params plus a return of absolutely nothing, because she fills in your pointers instead. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's the thing that makes this function lowkey iconic: when it converts color components out of pixel formats that have less than 8 bits per RGB component, it uses the entire 8-bit [0..255] range. she's not being stingy, she's stretching that range all the way out.

the example the docs give is so good: a completely white pixel in 16-bit RGB565 format would return [0xff, 0xff, 0xff] β€” not [0xf8, 0xfc, 0xf8].

so like, you get the full 255 treatment even though the source format couldn't technically represent it. that's the main character energy we're talking about. πŸ‘‘


πŸŽ€ FUNCTION PARAMETERS πŸŽ€

  • pixelvalue β€” a pixel value. yeah that's it, that's the description. a pixel. the value. give her the pixel. πŸ“
  • format β€” a pointer to SDL_PixelFormatDetails describing the pixel format. she needs to know what she's working with, bestie.
  • palette β€” an optional palette for indexed formats. may be NULL. optional!! she doesn't need it unless you're doing indexed formats. πŸ™ˆ
  • r β€” a pointer filled in with the red component. may be NULL.
  • g β€” a pointer filled in with the green component. may be NULL.
  • b β€” a pointer filled in with the blue component. may be NULL.
the way that r, g and b are all allowed to be NULL is so generous. don't care about blue? don't pass it. just want the green? iconic. just pass the pointers you actually want filled in. 🫢

πŸ’œ THREAD SAFETY πŸ’œ

it is safe to call this function from any thread, as long as the palette is not modified. so go off, thread away, just don't be out here editing the palette while somebody's reading from it. that's the one rule. 🧚


⭐ AVAILABILITY ⭐

this function is available since SDL 3.2.0. so if you're on anything older, no she's not there yet, sorry bestie 😭


🌈 SEE ALSO 🌈


okay that's the page!! go convert some pixels, you got this fr fr πŸ’–

this has been Simple Directmedia Layer, SDL 3.4.18, signing off on SDL_GetRGB(3). slay, bye bestie!! βœ¨πŸŽ€πŸ¦‹πŸ’…

πŸ“¦ where this page comes from