Does C++ use hex codes?

Started by
1 comment, last by rip-off 14 years, 11 months ago
I'm trying to do color keying (removing the background of an image) through a tutorial for Lazyfoo, but I'm kind of confused on how the colors work. Does C++ use hex codes the same as in HTML? The background of my image is red (FF0000) but I'm not sure how to find out what the hex code, or whatever C++ uses is. Any help is appreciated. :)
Advertisement
C++ has no notion of color. Representation of color depends on any additional libraries or APIs you might be using. C++ does have hexadecimal integer literals which are denoted with a prefix of 0x. 0xFF0000 is a legitimate int value. Whether or not it has anything to do with the color red depends on what you do with the int.
Yes and no.

Yes, C++ allows you to specify data in hexadecimal. However, C++ is too low level to understand pixel colours like HTML.

I am assuming that you are using SDL from your mention of Lazy Foo. SDL has a concept of pixel colours. It says that pixels are nominally 4 unsigned 8 bit values R,G,B,A. These values are 0 - 255, or if you want to use hex 0x00 - 0xff. The format of a pixel in a particular surface may vary with the hardware or operating system. However, SDL provides convenience functions to extract and manipulate these values. A pixel outside a surface is usually stored in a Uint32.

Lets say we want to fill the SDL_Surface "screen" with the colour red. Red is R,G,B (0xff, 0x00, 0x00).

We can write:
Uint32 red = SDL_MapRGB(screen->format, 0xff, 0x00, 0x00);SDL_FillRect(screen, NULL, red);


We can use such mapped RGB pixels for setting the colourkey of bitmaps, or even to read and write to the raw pixel data of a surface.

You may want to research SDL_MapRGB, SDL_MapRGBA, SDL_GetRGB, SDL_GetRGBA, SDL_SetColorKey, SDL_PixelFormat in the SDL documentation wiki.

This topic is closed to new replies.

Advertisement