/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* Color Context module
 * Copyright 1994,1995 John L. Cwikla
 * Copyright (C) 1997 by Ripley Software Development
 * Copyright (C) 1997 by Federico Mena (port to Gtk/Gdk)
 */

/* Copyright 1994,1995 John L. Cwikla
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appears in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of John L. Cwikla or
 * Wolfram Research, Inc not be used in advertising or publicity
 * pertaining to distribution of the software without specific, written
 * prior permission.  John L. Cwikla and Wolfram Research, Inc make no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * John L. Cwikla and Wolfram Research, Inc disclaim all warranties with
 * regard to this software, including all implied warranties of
 * merchantability and fitness, in no event shall John L. Cwikla or
 * Wolfram Research, Inc be liable for any special, indirect or
 * consequential damages or any damages whatsoever resulting from loss of
 * use, data or profits, whether in an action of contract, negligence or
 * other tortious action, arising out of or in connection with the use or
 * performance of this software.
 *
 * Author:
 *  John L. Cwikla
 *  X Programmer
 *  Wolfram Research Inc.
 *
 *  cwikla@wri.com
 */

#include <stdlib.h>
#include <string.h>
#include "gdk.h"
#include "gdkprivate.h"
#include "gdkx.h"

GdkColorContext the_cc;

void
gdk_init_color (void)
{
  gint cubeval;
  GdkColorContext *cc = &the_cc;
  
  cubeval = 2;
  
  cc->num_colors = cubeval * cubeval * cubeval;

#if 0  
  ccp->std_cmap.red_max    = cubeval - 1;
  ccp->std_cmap.green_max  = cubeval - 1;
  ccp->std_cmap.blue_max   = cubeval - 1;
  ccp->std_cmap.red_mult   = cubeval * cubeval;
  ccp->std_cmap.green_mult = cubeval;
  ccp->std_cmap.blue_mult  = 1;
  ccp->std_cmap.base_pixel = 0;
#endif
  
  cc->white_pixel = 0x07;
  cc->black_pixel = 0x00;
  cc->num_colors = 8;
  
  /* a CLUT for storing allocated pixel indices */
  
  cc->max_colors = cc->num_colors;
  cc->clut = g_new (gulong, cc->max_colors);
  
  for (cubeval = 0; cubeval < cc->max_colors; cubeval++)
    cc->clut[cubeval] = cubeval;
  
  cc->mode = GDK_CC_MODE_STD_CMAP;
}

GdkColorContext *
gdk_color_context_new (GdkVisual   *visual,
		       GdkColormap *colormap)
{
  return &the_cc;
}

GdkColorContext *
gdk_color_context_new_mono (GdkVisual   *visual,
			    GdkColormap *colormap)
{
  return &the_cc;
}

/* This doesn't currently free black/white, hmm... */

void
gdk_color_context_free (GdkColorContext *cc)
{
  g_assert (cc == &the_cc);
}

gulong
gdk_color_context_get_pixel (GdkColorContext *cc,
			     gushort          red,
			     gushort          green,
			     gushort          blue,
			     gint            *failed)
{
  g_assert (cc == &the_cc);
  g_assert (failed != NULL);
  
  *failed = FALSE;
  return  (red > 32768) * 4 +
          (green > 32768) * 2 +
          (blue > 32768);
}

void
gdk_color_context_get_pixels (GdkColorContext *cc,
			      gushort         *reds,
			      gushort         *greens,
			      gushort         *blues,
			      gint             ncolors,
			      gulong          *colors,
			      gint            *nallocated)
{
  *nallocated = 0;
}

void
gdk_color_context_get_pixels_incremental (GdkColorContext *cc,
					  gushort         *reds,
					  gushort         *greens,
					  gushort         *blues,
					  gint             ncolors,
					  gint            *used,
					  gulong          *colors,
					  gint            *nallocated)
{
  *nallocated = 0;
}

gint
gdk_color_context_query_color (GdkColorContext *cc,
			       GdkColor        *color)
{
  return gdk_color_context_query_colors (cc, color, 1);
}

gint
gdk_color_context_query_colors (GdkColorContext *cc,
				GdkColor        *colors,
				gint             num_colors)
{
  gint i;
  GdkColor *tc;
  
  g_assert (cc != NULL);
  g_assert (colors != NULL);
  
  for (i = 0, tc = colors; i < num_colors; i++, tc++)
    {
      tc->red = 65535 * (tc->pixel & 4);
      tc->green = 65535 * (tc->pixel & 2);
      tc->blue = 65535 * (tc->pixel & 1);
    }
  return 1;
}

gint
gdk_color_context_add_palette (GdkColorContext *cc,
			       GdkColor        *palette,
			       gint             num_palette)
{
  g_assert (cc != NULL);
  
  /* restore previous mode if we aren't adding a new palette */

  return 0;
}

void
gdk_color_context_init_dither (GdkColorContext *cc)
{
}

void
gdk_color_context_free_dither (GdkColorContext *cc)
{
}

gulong
gdk_color_context_get_pixel_from_palette (GdkColorContext *cc,
					  gushort         *red,
					  gushort         *green,
					  gushort         *blue,
					  gint            *failed)
{
  *failed = TRUE;
  return 0;
}

guchar
gdk_color_context_get_index_from_palette (GdkColorContext *cc,
					  gint            *red,
					  gint            *green,
					  gint            *blue,
					  gint            *failed)
{
  *failed=TRUE;
  return 0;
}

