How to compare colors? (Allegro 5)
hdlopesrocha

Hello everybody! I am trying to convert my project from A4 to A5 but I have a little problem.

ALLEGRO 4.2.2

#define MAGENTA makecol (255,0,255)

int x_empty(BITMAP * bmp,int x){
int y;

for(y=0; y<bmp->h; ++y){
if(getpixel(bmp,x,y)!=MAGENTA)
return 0;
}
return 1;
}

ALLEGRO 5

#define MAGENTA al_map_rgb(255,0,255)

int x_empty(ALLEGRO_BITMAP * bmp,int x){
int y;

for(y=0; y<al_get_bitmap_height(bmp); ++y){
if(al_get_pixel(bmp,x,y)!=MAGENTA)
return 0;
}
return 1;
}

I am having this error:
era.c: In function ‘x_empty’:
era.c:23:27: error: invalid operands to binary != (have ‘ALLEGRO_COLOR’ and ‘ALLEGRO_COLOR’)

I have this include:
#include <allegro5/allegro_color.h>

And I compile like this:
gcc -o era main.c era.c $(pkg-config --libs allegro_font-5.0 allegro_image-5.0 allegro_color-5.0 allegro_audio-5.0)

I don't know where is the problem. Can someone help me?
Thanks ;)

Matthew Leverton

You can memcmp the two:

if (!memcmp(&c1, &c2, sizeof(ALLEGRO_COLOR))) // same

However, if you want more of a fuzzy match since they are floats, you can just do:

float D = 0.0001;
if (abs(c1.r - c2.r) < D && abs(c1.g - c2.g) < D && abs(c1.b - c2.b) < D) 
  // same

Obviously you can also compare alpha if you want. (The memcmp version does.)

hdlopesrocha

Thanks ;)

Johan Halmén

My guess is that in A5, ALLEGRO_COLOR is a struct, not a single 32 bit int, as the colours in A4 were.

TFM said:

typedef struct ALLEGRO_COLOR ALLEGRO_COLOR;

An ALLEGRO_COLOR structure describes a color in a device independant way. Use al_map_rgb et al.
and al_unmap_rgb et al. to translate from and to various color representations.

  • beaten

Matthew Leverton

It's one of the few structs that is not opaque:

struct ALLEGRO_COLOR
{
   float r, g, b, a;
}

So you don't really need to use the unmap functions if you just want to compare floats.

Mark Oates

That's a cool trick.

Thread #607895. Printed from Allegro.cc