1

I already learnt the working mechanis of RGBA, so I realised that hexadecimal numbers that belongs to the different colours can be turned into simple integers. It means that I can store colours in integers, even though some of them are quite big.

My question is that how can I get the colour that belongs to an integer I give to the program?

EDIT: of course I forgot to mention that I use Allegro too but I'm new in it...are there any functions of whatever that can do it?

Zoltán Schmidt
  • 1,286
  • 2
  • 28
  • 48

1 Answers1

0

It sounds like you are using Allegro 4 if you are storing colors as integers. It provides a variety of functions for you to use, just check out the manual.

// int makecol(int r, int g, int b);

int white = makecol(255, 255, 255);
int green = makecol(0, 255, 0);

Or the inverse:

int r = getr(color);
int g = getg(color);
int b = getb(color);

With Allegro 4, the ordering depends on the graphics card. So the return value of makecol() can be different for the same color depending if it is stored RGB or BGR. So you must use the above functions to get the proper color values, and only after setting the graphics mode.

If using Allegro 5 (which I highly recommend over Allegro 4), you use the ALLEGRO_COLOR struct, which hides the underlying implementation details and thus none of the above is applicable.

Matthew
  • 47,584
  • 11
  • 86
  • 98
  • Yes, I'm using Allegro 5. =) but you still don't get what I mean: I want to store the colour with ONE number instead of THREE, because handling three numbers is more risky than only one for each elements. But of course, thanks for the help! – Zoltán Schmidt May 19 '13 at 23:43
  • @ZoltánSchmidt, all of the Allegro 5 routines require you to use an `ALLEGRO_COLOR` struct, so storing them as single integers just doesn't make any sense. That said, the simplest integer representation of RGB would be `int c = ((r<<16)|(g<<8)|b)`, where each component is 0-255. To extract, shift the other direction and mask: `int g = (c>>8) & 0xff` – Matthew May 20 '13 at 13:45