SDL_MapRGBA(3)

πŸ’– SDL_MapRGBA β€” the glow-up your colors deserve πŸ’–

okay bestie, so this is a page from the SDL3 FUNCTIONS collection, and it's literally about turning a whole RGBA quadruple into a single pixel value for whatever pixel format you're working with. no cap. ✨


🌸 NAME 🌸

SDL_MapRGBA β€” Map an RGBA quadruple to a pixel value for a given pixel format.

like... you give it a color, it gives you back the pixel. it's giving translator. it's giving matchmaker. 🫢


✨ SYNOPSIS ✨

#include <SDL3/SDL_pixels.h>

Uint32 SDL_MapRGBA(const SDL_PixelFormatDetails *format, const SDL_Palette *palette, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's the tea, this function maps the RGBA color value to the specified pixel format and returns the pixel value that best approximates the given RGBA color value for the given pixel format. it's literally out here doing its best, and honestly? its best is great. πŸ”₯

if the pixel format you picked has no alpha component, then the alpha value gets straight-up ignored (same energy in formats with a palette, so don't come for it).

if the format has a palette (8-bit), you get back the index of the closest matching color in that palette. it's shopping the palette for you. icon behavior. πŸ‘‘

and if the pixel format's bpp (color depth) is less than 32-bpp, then the unused upper bits of the return value can safely be ignored β€” like, for example, with a 16-bpp format the return value can be assigned to a Uint16, and same vibe with a Uint8 for an 8-bpp format. the extra bits are just hanging out, they're not doing anything, ignore them. πŸ’…


πŸŽ€ FUNCTION PARAMETERS πŸŽ€

  • format β€” a pointer to SDL_PixelFormatDetails describing the pixel format. this is the blueprint, the vibe check, the whole moment.
  • palette β€” an optional palette for indexed formats, may be NULL. optional!! she can be absent and that's fine.
  • r β€” the red component of the pixel in the range 0-255.
  • g β€” the green component of the pixel in the range 0-255.
  • b β€” the blue component of the pixel in the range 0-255.
  • a β€” the alpha component of the pixel in the range 0-255.

⭐ RETURN VALUE ⭐

Returns a pixel value. that's it, that's the whole thing. short queen. πŸ’…


🧚 THREAD SAFETY 🧚

It is safe to call this function from any thread, as long as the palette is not modified. so like β€” multitask all you want, but do NOT go changing the palette out from under everyone. that's the one rule fr fr. πŸ™ˆ


🌈 AVAILABILITY 🌈

This function is available since SDL 3.2.0. she's been around. she's established. periodt.


πŸ‘‘ SEE ALSO πŸ‘‘

go read those too, iykyk πŸ’œ


ngl bestie, if your format has no alpha, your alpha is getting ignored and that's not a bug, that's the design. don't @ me. πŸ’€

and that's the whole page, mwah πŸ’‹ this one came from the lovely people at Simple Directmedia Layer, living in SDL 3.4.18 β€” so go map some RGBA and slay, okay? bye babe πŸŽ€βœ¨πŸ’–

πŸ“¦ where this page comes from