ALLEGRO_COLOR for noobs
kingnoob

I'm new to Allegro. And I had a hard time figuring out how to make and array of ALLEGRO_COLOR structures. I figured it out in probably a very noobish way. Here it is but maybe someone will show how it can be done better, idk.

First define the colors.

#define BLACK al_map_rgb(0, 0 ,0)
#define RED al_map_rgb(255, 0, 0)
#define GREEN al_map_rgb(0, 255, 0)
#define BLUE al_map_rgb(0, 0, 255)
#define DARKRED al_map_rgb(128,0,0)
#define DARKGREEN al_map_rgb(0, 128, 0)
#define DARKBLUE al_map_rgb(0, 0, 128)
#define PINK al_map_rgb(255, 20, 147)
#define ORANGE al_map_rgb(255, 140, 0)
#define YELLOW al_map_rgb(255, 255, 0)
#define PURPLE al_map_rgb(147, 112, 219)
#define BROWN al_map_rgb(160, 82, 45)
#define BEIGE al_map_rgb(210, 180, 140)
#define LIGHTGRAY al_map_rgb(211, 211, 211)
#define DARKGRAY al_map_rgb(105, 105, 105)
#define WHITE al_map_rgb(250, 250, 250)

Then create the global ALLEGRO_COLOR structure array.

struct ALLEGRO_COLOR color[MAX_COLORS];

Then in Initialize() after al_init().

struct ALLEGRO_COLOR c[] =
{ BLACK,DARKBLUE,DARKRED,DARKGREEN,PINK,YELLOW,ORANGE,PURPLE,BLUE,RED,GREEN,BEIGE,BROWN };
for (int i = 0; i < MAX_COLORS; i++) color[i] = c[i];

And now one can use the global array as in color[i] or just use a color define as in RED wherever an ALLEGRO_COLOR is needed.

amarillion

Isn't that redundant? Why don't you define the array directly like so:

struct ALLEGRO_COLOR color[] = { BLACK,DARKBLUE,DARKRED,DARKGREEN,PINK,YELLOW,ORANGE,PURPLE,BLUE,RED,GREEN,BEIGE,BROWN };

No need to copy the data.

Peter Hull

Why don't you

You can't call al_map_rgb before al_init so it doesn't work as a global initialiser. (actually the reason why you can't is a bit disappointing and could be fixed quite easily I think)

Chris Katko

(actually the reason why you can't is a bit disappointing and could be fixed quite easily I think)

I thought it was because the color format could change based on what system you're on.

SiegeLord

(actually the reason why you can't is a bit disappointing and could be fixed quite easily I think)

https://github.com/liballeg/allegro5/pull/1217

Peter Hull

Nice one SL!

I thought it was because the color format could change based on what system you're on.

So did I until recently; I was probably thinking of makecol from Allegro 4.

ALLEGRO_COLOR is just a 16-byte struct of four floats R, G, B and A. al_map_rgb converts each integer 0..255 to a float using a lookup table and it's that table that needs to be initialised by al_init (that is, until PR #1217!)

Elias

I think even the color addon will work with this change, so al_color_name("red") should be callable before main as well now (and without initializing the color addon). Not sure there's much reason to advertise it in the documentation but I may start doing it from now on in some test code :)

Thread #618335. Printed from Allegro.cc