From 314e8c9222f64b1520e384ca7a0e03a2c51d44c5 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 1 Apr 2012 21:57:19 -0700 Subject: [PATCH] Add palette find function. --- libguac/include/palette.h | 1 + libguac/src/palette.c | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/libguac/include/palette.h b/libguac/include/palette.h index fc3cc3dd..fb30066e 100644 --- a/libguac/include/palette.h +++ b/libguac/include/palette.h @@ -56,6 +56,7 @@ typedef struct guac_palette { } guac_palette; guac_palette* guac_palette_alloc(cairo_surface_t* surface); +int guac_palette_find(guac_palette* palette, int color); void guac_palette_free(guac_palette* palette); #endif diff --git a/libguac/src/palette.c b/libguac/src/palette.c index 2add6b1a..dc18ebb8 100644 --- a/libguac/src/palette.c +++ b/libguac/src/palette.c @@ -111,6 +111,33 @@ guac_palette* guac_palette_alloc(cairo_surface_t* surface) { } +int guac_palette_find(guac_palette* palette, int color) { + + /* Calculate hash code */ + int hash = ((color & 0xFFF000) >> 12) ^ (color & 0xFFF); + + guac_palette_entry* entry; + + /* Search for palette entry */ + for (;;) { + + entry = &(palette->entries[hash]); + + /* If we've found a free space, color not stored. */ + if (entry->index == 0) + return -1; + + /* Otherwise, if color indeed stored here, done */ + if (entry->color == color) + return entry->index; + + /* Otherwise, collision. Move on to another bucket */ + hash = (hash+1) & 0xFFF; + + } + +} + void guac_palette_free(guac_palette* palette) { free(palette); }