+static unsigned long AllocColorCell(int r, int g, int b)
+{
+ byte pixel_mapping = 0;
+ int i;
+
+ r >>= 10;
+ g >>= 10;
+ b >>= 10;
+
+ /* try to use existing colors from the global colormap */
+ for (i=0; i<global_colormap_entries_used; i++)
+ {
+ if (r == global_colormap[i].r &&
+ g == global_colormap[i].g &&
+ b == global_colormap[i].b) /* color found */
+ {
+ pixel_mapping = i;
+ break;
+ }
+ }
+
+ if (i == global_colormap_entries_used) /* color not found */
+ {
+ if (global_colormap_entries_used < MAX_COLORS)
+ global_colormap_entries_used++;
+
+ global_colormap[i].r = r;
+ global_colormap[i].g = g;
+ global_colormap[i].b = b;
+
+ pixel_mapping = i;
+ }
+
+ return pixel_mapping;
+}
+