1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
|
|
|
/* under the terms of the GNU Library General Public License. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-08 10:04:35 -07:00
|
|
|
#include "libgraph.h"
|
|
|
|
|
|
|
|
/* Cache to speed up the translation rgb -> pixel value. */
|
|
|
|
|
|
|
|
struct color_cache_entry {
|
|
|
|
int rgb; /* RGB value with format 0xRRGGBB */
|
|
|
|
unsigned long pixel; /* Pixel value */
|
|
|
|
};
|
|
|
|
|
1999-01-27 10:21:14 -08:00
|
|
|
#define Color_cache_size 512
|
1995-05-08 10:04:35 -07:00
|
|
|
static struct color_cache_entry color_cache[Color_cache_size];
|
|
|
|
#define Empty (-1)
|
|
|
|
#define Hash_rgb(r,g,b) \
|
1999-01-27 10:21:14 -08:00
|
|
|
((((r) & 0xE0) << 1) + (((g) & 0xE0) >> 2) + (((b) & 0xE0) >> 5))
|
|
|
|
#define Color_cache_slack 16
|
|
|
|
|
|
|
|
static int num_overflows = 0;
|
1995-05-08 10:04:35 -07:00
|
|
|
|
1999-02-19 09:08:27 -08:00
|
|
|
Bool direct_rgb = False;
|
|
|
|
int byte_order;
|
|
|
|
int bitmap_unit;
|
|
|
|
int bits_per_pixel;
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void gr_init_color_cache(void)
|
1995-05-08 10:04:35 -07:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < Color_cache_size; i++) color_cache[i].rgb = Empty;
|
|
|
|
i = Hash_rgb(0, 0, 0);
|
|
|
|
color_cache[i].rgb = 0;
|
|
|
|
color_cache[i].pixel = grblack;
|
|
|
|
i = Hash_rgb(0xFF, 0xFF, 0xFF);
|
|
|
|
color_cache[i].rgb = 0xFFFFFF;
|
|
|
|
color_cache[i].pixel = grwhite;
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
unsigned long gr_pixel_rgb(int rgb)
|
1995-05-08 10:04:35 -07:00
|
|
|
{
|
|
|
|
unsigned int r, g, b;
|
|
|
|
int h, i;
|
|
|
|
XColor color;
|
1999-02-19 09:08:27 -08:00
|
|
|
unsigned short tmp;
|
1995-05-08 10:04:35 -07:00
|
|
|
|
|
|
|
r = (rgb >> 16) & 0xFF;
|
|
|
|
g = (rgb >> 8) & 0xFF;
|
|
|
|
b = rgb & 0xFF;
|
1999-02-19 09:08:27 -08:00
|
|
|
|
|
|
|
if (direct_rgb){
|
|
|
|
switch ( bits_per_pixel ){
|
|
|
|
case 16:
|
2000-04-05 11:30:22 -07:00
|
|
|
tmp = ((r >> 3) << 11) + ((g >> 2) << 5) + ((b >> 3) << 0);
|
|
|
|
return (unsigned long) tmp;
|
1999-02-19 09:08:27 -08:00
|
|
|
case 32:
|
|
|
|
return (r << 16) + (g << 8) + (b << 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-05-08 10:04:35 -07:00
|
|
|
h = Hash_rgb(r, g, b);
|
|
|
|
i = h;
|
|
|
|
while(1) {
|
|
|
|
if (color_cache[i].rgb == Empty) break;
|
|
|
|
if (color_cache[i].rgb == rgb) return color_cache[i].pixel;
|
|
|
|
i = (i + 1) & (Color_cache_size - 1);
|
1999-01-27 10:21:14 -08:00
|
|
|
if (i == h) {
|
2000-04-05 11:30:22 -07:00
|
|
|
/* Cache is full. Instead of inserting at slot h, which causes
|
|
|
|
thrashing if many colors hash to the same value,
|
|
|
|
insert at h + n where n is pseudo-random and
|
|
|
|
smaller than Color_cache_slack */
|
|
|
|
int slack = num_overflows++ & (Color_cache_slack - 1);
|
|
|
|
i = (i + slack) & (Color_cache_size - 1);
|
|
|
|
break;
|
1999-01-27 10:21:14 -08:00
|
|
|
}
|
1995-05-08 10:04:35 -07:00
|
|
|
}
|
|
|
|
color.red = r * 0x101;
|
|
|
|
color.green = g * 0x101;
|
|
|
|
color.blue = b * 0x101;
|
|
|
|
XAllocColor(grdisplay, grcolormap, &color);
|
|
|
|
color_cache[i].rgb = rgb;
|
|
|
|
color_cache[i].pixel = color.pixel;
|
|
|
|
return color.pixel;
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
int gr_rgb_pixel(long unsigned int pixel)
|
1995-05-08 10:04:35 -07:00
|
|
|
{
|
|
|
|
XColor color;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (pixel == grblack) return 0;
|
|
|
|
if (pixel == grwhite) return 0xFFFFFF;
|
|
|
|
|
|
|
|
/* Probably faster to do a linear search than to query the X server. */
|
|
|
|
for (i = 0; i < Color_cache_size; i++) {
|
|
|
|
if (color_cache[i].rgb != Empty && color_cache[i].pixel == pixel)
|
|
|
|
return color_cache[i].rgb;
|
|
|
|
}
|
|
|
|
color.pixel = pixel;
|
|
|
|
XQueryColor(grdisplay, grcolormap, &color);
|
|
|
|
return
|
|
|
|
((color.red >> 8) << 16) + ((color.green >> 8) << 8) + (color.blue >> 8);
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value gr_set_color(value vrgb)
|
1995-05-08 10:04:35 -07:00
|
|
|
{
|
|
|
|
gr_check_open();
|
|
|
|
grcolor = gr_pixel_rgb(Int_val(vrgb));
|
|
|
|
XSetForeground(grdisplay, grwindow.gc, grcolor);
|
|
|
|
XSetForeground(grdisplay, grbstore.gc, grcolor);
|
|
|
|
return Val_unit;
|
|
|
|
}
|