# SDL_MapRGB - what is a pixel value?

The wiki page for SDL_MapRGB states that it returns a “pixel value”.

Could someone explain to me what a pixel value is in this case?

When i do

``````cout<<"The value of SDL_MapRGB is: "<<SDL_MapRGB(boatIMG->format,255,255,255)<<endl;
``````

it puts out a 10-digit number: 4294967295

Could anyone explain what this means?

What I am hoping to do is obtain the colour of particular pixels given the pixels’ individual coordinates. I haven’t worked out how to do this yet, but I’m hoping working out this function will help.

Thanks for any help.

it puts out a 10-digit number: 4294967295

Could anyone explain what this means?

This is value that represents white (full red, green, blue) on `boatIMG`.

It’s more helpful to look at that number as hexadecimal: 0xFFFFFFFF …or, split apart: FF FF FF FF (that is: red, green, blue and alpha are all at full intensity).

Since that surface is using 32 bits per pixel, each color component got 8 bits. If you had a 16-bit surface, you’d have gotten a different value here, and if you had something other than white and the color components were in a different order in this surface, that value would have the bits swapped around.

If you want the value of a specific pixel on your surface, you’ll have to find it in the surface:

``````// this is completely untested and unoptimized!
Uint32 getPixel(SDL_Surface *s, int x, int y)
{
const Uint8 *p = (const Uint8 *) s->pixels;
p += (y * s->pitch);  // find the start of the row.
p += (x * s->format->BytesPerPixel);
if (s->format->BytesPerPixel == 1)
return (Uint32) *p;
else if (s->format->BytesPerPixel == 2)
return (Uint32) *((Uint16 *) p);
else if (s->format->BytesPerPixel == 4)
return *((Uint32 *) p);
return 0;  // uhoh.
}
``````

Once you have this, you can split it into red/green/blue/alpha and let SDL deal with the specifics of what that pixel value means for the surface:

``````const Uint32 pixel = getPixel(surface, 0, 0);  // top left pixel.
Uint8 r,g,b,a;
SDL_GetRGBA(pixel, surface->format, &r, &g, &b, &a);
// r,g,b,a have values from 0 to 255 representing color components.
``````

–ryan.

1 Like