scx/c/xlib/color.c

90 lines
2.3 KiB
C

#include "xlib.h"
#include "scheme48.h"
s48_value Create_Color(s48_value r, s48_value g, s48_value b) {
s48_value col = S48_MAKE_VALUE(XColor);
XColor* c = S48_EXTRACT_VALUE_POINTER(col, XColor);
c->red = s48_extract_integer(r);
c->green = s48_extract_integer(g);
c->blue = s48_extract_integer(b);
return col;
}
s48_value Int_Extract_RGB_Values(XColor col) {
s48_value res = S48_NULL;
S48_DECLARE_GC_PROTECT(1);
S48_GC_PROTECT_1(res);
res = s48_cons( s48_enter_integer(col.blue), res );
res = s48_cons( s48_enter_integer(col.green), res );
res = s48_cons( s48_enter_integer(col.red), res );
S48_GC_UNPROTECT();
return res;
}
s48_value Extract_RGB_Values(s48_value Xcolor) {
return Int_Extract_RGB_Values(*EXTRACT_COLOR(Xcolor));
}
s48_value Query_Color (s48_value Xcolormap, s48_value Xpixel,
s48_value Xdisplay) {
XColor c;
c.pixel = EXTRACT_PIXEL(Xpixel);
XQueryColor(EXTRACT_DISPLAY(Xdisplay), EXTRACT_COLORMAP(Xcolormap), &c);
return Int_Extract_RGB_Values(c);
}
s48_value Query_Colors(s48_value Xcolormap, s48_value Xpixels,
s48_value Xdisplay) {
s48_value result;
S48_DECLARE_GC_PROTECT(1);
long n = S48_VECTOR_LENGTH(Xpixels);
XColor p[n];
int i;
for (i=0; i < n; i++)
p[i].pixel = EXTRACT_PIXEL(S48_VECTOR_REF(Xpixels, i));
XQueryColors( EXTRACT_DISPLAY(Xdisplay), EXTRACT_COLORMAP(Xcolormap), p, n );
S48_GC_PROTECT_1(result);
result = s48_make_vector(n, S48_FALSE);
for (i=0; i < n; i++)
S48_VECTOR_SET(result, i, Int_Extract_RGB_Values(p[i]));
S48_GC_UNPROTECT();
return result;
}
s48_value Lookup_Color(s48_value Xcolormap, s48_value Xdisplay,
s48_value color_name) {
XColor visual, exact;
s48_value res = S48_FALSE;
S48_DECLARE_GC_PROTECT(1);
if (XLookupColor( EXTRACT_DISPLAY(Xdisplay), EXTRACT_COLORMAP(Xcolormap),
s48_extract_string(color_name), &visual, &exact )) {
S48_GC_PROTECT_1(res);
res = s48_cons( Int_Extract_RGB_Values( visual ),
Int_Extract_RGB_Values( exact ) );
}
S48_GC_UNPROTECT();
return res;
}
void s48_init_color(void) {
S48_EXPORT_FUNCTION(Create_Color);
S48_EXPORT_FUNCTION(Extract_RGB_Values);
S48_EXPORT_FUNCTION(Query_Color);
S48_EXPORT_FUNCTION(Query_Colors);
S48_EXPORT_FUNCTION(Lookup_Color);
}