Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » ALLEGRO_COLOR for noobs

This thread is locked; no one can reply to it. rss feed Print
ALLEGRO_COLOR for noobs
kingnoob
Member #18,984
January 2021

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
Member #940
January 2001
avatar

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
Member #1,136
March 2001

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
Member #1,881
January 2002
avatar

(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.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

SiegeLord
Member #7,827
October 2006
avatar

(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

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Peter Hull
Member #1,136
March 2001

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
Member #358
May 2000

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 :)

--
"Either help out or stop whining" - Evert

Go to: