/* xyz */

/*
 * Copyright (c) 1994, 1995, 1997 by Wayne C. Gramlich.
 * All rights reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * for any purpose is hereby granted without fee provided that the above
 * copyright notice and this permission are retained.  The author makes
 * no representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 */

/*
 * This file provide a bunch of interface routines for dealing xlib
 * {Colormap} objects.
 */

#ifndef XLIB_H
#include "xlib.h"
#endif

/* The initial object: */
Xlib_color_map_struct xlib_color_map___initial_object = {
    &xlib_screen___initial_object,	/* Screen color map is from */
    0					/* Pad the rest with zeros */
};
Xlib_color_map xlib_color_map___initial = &xlib_color_map___initial_object;

/*
 * xlib_color_map__address_get(color_map)
 *	This routine will return the address of {color_map}.
 */
unsigned
xlib_color_map__address_get(
    Xlib_color_map color_map)
{
    return (unsigned)color_map;
}
    
/*
 * xlib_color_map__external__initialize()
 *	This routine will initialize the {Xlib_color_map} object:
 */
void
xlib_color_map__external__initialize(void)
{
    assert(xlib_color_map___initial == &xlib_color_map___initial_object);
}

/*
 * xlib_color_map__color_allocate(color_map, color)
 *	This procedure will allocate the closest color to {color} in
 *	{color_map}.  If a color is successfully allocated, the color
 *	values are stored and color id are stored into {color} and
 *	{false} is returned.  If no color is allocated, {false} is
 *	returned.
 */
int
xlib_color_map__color_allocate(
    Xlib_color_map color_map,
    Xlib_color color)
{
    Xlib_display display;

    display = color_map->screen->display;
    if (display->closed) {
	xlib_display___closed(display);
    } else {
	if (XAllocColor(display->display,
	  color_map->color_map, &color->color) != 0) {
	    return 0;
	}
    }
    return 1;
}

/*
 * xlib_color_map__create(screen, visual, allocate_color_map)
 *	This procedure will create and return an {xlib_color_map}
 *	object.  The returned color map will will be associated
 *	with {screen} and {visual}.  If {allocate_color_map} is
 *	{true}, all of the color map entries are allocated as read/write
 *	color map cells; otherwise, none are allocated.  ?? is return
 *	if any errors occur, or the color map can not be allocated to
 *	the specified screen and visual.
 */
Xlib_color_map
xlib_color_map__create(
    Xlib_screen screen,
    Xlib_visual visual,
    int allocate_color_map)
{
    Xlib_color_map color_map;
    Xlib_display display;

    color_map = xlib_color_map___initial;
    display = screen->display;
    if (display->closed) {
	xlib_display___closed(display);
    } else {
	color_map = (Xlib_color_map)malloc(sizeof(*color_map));
	color_map->screen = screen;
	color_map->color_map =
	  XCreateColormap(display->display, screen->root_window->window,
	  visual->visual.visual, allocate_color_map ? AllocAll : AllocNone);
    }
    return color_map;
}


/*
 * xlib_color_map__lookup(color_map, color_name)
 *	This routine will lookup {color_name} for use with {color}.
 */
Xlib_color
xlib_color_map__lookup(
    Xlib_color_map color_map,
    String color_name)
{
    Xlib_color color;
    Xlib_display display;
    Str name;

    color = xlib_color___initial;
    display = color_map->screen->display;
    if (display->closed) {
	xlib_display___closed(display);
    } else {
	color = (Xlib_color)malloc(sizeof(*color));
	color->color_map = color_map;
	name = string__unix_string(color_name);
	if (!XParseColor(display->display,
	  color_map->color_map, name, &color->color)) {
	    xlib_errors___add(display->errors, "Could not lookup color name!");
	    return color;
	}
	if (!XAllocColor(display->display,
	  color_map->color_map, &color->color)) {
	    xlib_errors___add(display->errors, "Unable to allocate color!");
	    return color;
	}
    }
    return color;
}

/*
 * xlib_screen__color_map_default_get(screen)
 *	This routine will return the color map assocated with {screen}.
 */
Xlib_color_map
xlib_screen__color_map_default_get(
    Xlib_screen screen)
{
    Xlib_color_map color_map;
    Xlib_display display;

    color_map = xlib_color_map___initial;
    display = screen->display;
    if (display->closed) {
	xlib_display___closed(display);
    } else {
	color_map = (Xlib_color_map)malloc(sizeof(*color_map));
	color_map->screen = screen;
	color_map->color_map =
	  DefaultColormap(display->display, screen->number);
    }
    return color_map;
}




