Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How to compare colors? (Allegro 5)

This thread is locked; no one can reply to it. rss feed Print
How to compare colors? (Allegro 5)
Henrique Rocha
Member #13,042
July 2011

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
Supreme Loser
January 1999
avatar

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

Henrique Rocha
Member #13,042
July 2011

Thanks ;)

Johan Halmén
Member #1,550
September 2001

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Matthew Leverton
Supreme Loser
January 1999
avatar

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

That's a cool trick.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Go to: