PROGRAM(GdkColor);
// The GDK.Color object is used to represent a color.
// When you call GDK.Color(r,g,b) the color will be allocated 
// from the X-server. The constructor can return an  exception if there are
// no more colors to allocate.
//
FUNCTION(create, "function(int,int,int:void)")
NAME_ARGS(r,g,b);
// r g and b are in the range 0 to 255, inclusive
{
  int r,g,b;
  GdkColormap *col = gdk_colormap_get_system();
  GdkColor *c;

  get_all_args("GdkColor", args, "%d%d%d", &r, &g, &b);
  c = malloc(sizeof(GdkColor));
  THIS->obj = (void *)c;
  c->red   = r * 257;
  c->green = g * 257;
  c->blue  = b * 257;
  c->pixel = 0;
  if(!gdk_color_alloc( col, c ))
  {
    free(c);
    THIS->obj = 0;
    error("Failed to allocate color.\n");
  }
}

FUNCTION(rgb, "function(void:array(int))")
// Returns the red green and blue color components as an array.
{
  push_int(((GdkColor *)THIS->obj)->red/257);
  push_int(((GdkColor *)THIS->obj)->green/257);
  push_int(((GdkColor *)THIS->obj)->blue/257);
  f_aggregate(3);
}

FUNCTION(red, "function(void:int)")
// Returns the red color component.
{
  push_int(((GdkColor *)THIS->obj)->red/257);
}

FUNCTION(green, "function(void:int)")
// Returns the green color component.
{
  push_int(((GdkColor *)THIS->obj)->green/257);
}

FUNCTION(blue, "function(void:int)")
// Returns the blue color component.
{
  push_int(((GdkColor *)THIS->obj)->blue/257);
}

FUNCTION(pixel, "function(void:int)")
// Returns the pixel value of the color. See <a href=GdkImage.html#set_pixel>GDK.Image->set_pixel</a>.
{
  push_int(((GdkColor *)THIS->obj)->pixel);
}

FUNCTION(destroy, "function(void:void)")
// Destroys the color object. Please note that this function does
// _not_ free the color from the X-colormap right now.
{
  if(THIS->obj)
  {
    free(THIS->obj);
    THIS->obj = 0;
  }
}
