gdkcolor.c 28.7 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19
#include <time.h>
Elliot Lee's avatar
Elliot Lee committed
20 21 22
#include <X11/Xlib.h>
#include "gdk.h"
#include "gdkprivate.h"
Tim Janik's avatar
Tim Janik committed
23
#include "gdkx.h"
Elliot Lee's avatar
Elliot Lee committed
24 25 26 27 28 29 30 31 32 33


static gint  gdk_colormap_match_color (GdkColormap *cmap,
				       GdkColor    *color,
				       const gchar *available);
static void  gdk_colormap_add         (GdkColormap *cmap);
static void  gdk_colormap_remove      (GdkColormap *cmap);
static guint gdk_colormap_hash        (Colormap    *cmap);
static gint  gdk_colormap_cmp         (Colormap    *a,
				       Colormap    *b);
34
static void gdk_colormap_real_destroy (GdkColormap *colormap);
Elliot Lee's avatar
Elliot Lee committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

static GHashTable *colormap_hash = NULL;


GdkColormap*
gdk_colormap_new (GdkVisual *visual,
		  gint       private_cmap)
{
  GdkColormap *colormap;
  GdkColormapPrivate *private;
  Visual *xvisual;
  int size;
  int i;

  g_return_val_if_fail (visual != NULL, NULL);

  private = g_new (GdkColormapPrivate, 1);
  colormap = (GdkColormap*) private;

  private->xdisplay = gdk_display;
  private->visual = visual;
  private->ref_count = 1;
57 58 59 60 61

  private->hash = NULL;
  private->last_sync_time = 0;
  private->info = NULL;
  
Elliot Lee's avatar
Elliot Lee committed
62 63
  xvisual = ((GdkVisualPrivate*) visual)->xvisual;

64 65 66
  colormap->size = visual->colormap_size;
  colormap->colors = g_new (GdkColor, colormap->size);

Elliot Lee's avatar
Elliot Lee committed
67 68 69 70
  switch (visual->type)
    {
    case GDK_VISUAL_GRAYSCALE:
    case GDK_VISUAL_PSEUDO_COLOR:
71 72 73
      private->info = g_new0 (GdkColorInfo, colormap->size);
      colormap->colors = g_new (GdkColor, colormap->size);
      
74 75
      private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
					(GCompareFunc) gdk_color_equal);
76
      
Elliot Lee's avatar
Elliot Lee committed
77 78 79 80 81 82
      private->private_val = private_cmap;
      private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
					    xvisual, (private_cmap) ? (AllocAll) : (AllocNone));

      if (private_cmap)
	{
83 84 85 86 87
	  XColor *default_colors;

	  default_colors = g_new (XColor, colormap->size);

	  for (i = 0; i < colormap->size; i++)
Elliot Lee's avatar
Elliot Lee committed
88 89 90 91
	    default_colors[i].pixel = i;

	  XQueryColors (private->xdisplay,
			DefaultColormap (private->xdisplay, gdk_screen),
92
			default_colors, colormap->size);
Elliot Lee's avatar
Elliot Lee committed
93

94
	  for (i = 0; i < colormap->size; i++)
Elliot Lee's avatar
Elliot Lee committed
95 96 97 98 99 100 101
	    {
	      colormap->colors[i].pixel = default_colors[i].pixel;
	      colormap->colors[i].red = default_colors[i].red;
	      colormap->colors[i].green = default_colors[i].green;
	      colormap->colors[i].blue = default_colors[i].blue;
	    }

102 103 104
	  gdk_colormap_change (colormap, colormap->size);
	  
	  g_free (default_colors);
Elliot Lee's avatar
Elliot Lee committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	}
      break;

    case GDK_VISUAL_DIRECT_COLOR:
      private->private_val = TRUE;
      private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
					    xvisual, AllocAll);

      size = 1 << visual->red_prec;
      for (i = 0; i < size; i++)
	colormap->colors[i].red = i * 65535 / (size - 1);

      size = 1 << visual->green_prec;
      for (i = 0; i < size; i++)
	colormap->colors[i].green = i * 65535 / (size - 1);

      size = 1 << visual->blue_prec;
      for (i = 0; i < size; i++)
	colormap->colors[i].blue = i * 65535 / (size - 1);

125
      gdk_colormap_change (colormap, colormap->size);
Elliot Lee's avatar
Elliot Lee committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
      break;

    case GDK_VISUAL_STATIC_GRAY:
    case GDK_VISUAL_STATIC_COLOR:
    case GDK_VISUAL_TRUE_COLOR:
      private->private_val = FALSE;
      private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
					    xvisual, AllocNone);
      break;
    }

  gdk_colormap_add (colormap);

  return colormap;
}

142
static void
Elliot Lee's avatar
Elliot Lee committed
143 144 145 146 147
gdk_colormap_real_destroy (GdkColormap *colormap)
{
  GdkColormapPrivate *private = (GdkColormapPrivate*) colormap;

  g_return_if_fail (colormap != NULL);
148
  g_return_if_fail (private->ref_count == 0);
Elliot Lee's avatar
Elliot Lee committed
149 150 151

  gdk_colormap_remove (colormap);
  XFreeColormap (private->xdisplay, private->xcolormap);
152 153 154 155 156

  if (private->hash)
    g_hash_table_destroy (private->hash);
  
  g_free (private->info);
157
  g_free (colormap->colors);
Elliot Lee's avatar
Elliot Lee committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  g_free (colormap);
}

GdkColormap*
gdk_colormap_ref (GdkColormap *cmap)
{
  GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
  g_return_val_if_fail (cmap != NULL, NULL);

  private->ref_count += 1;
  return cmap;
}

void
gdk_colormap_unref (GdkColormap *cmap)
{
  GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
  g_return_if_fail (cmap != NULL);

  private->ref_count -= 1;
  if (private->ref_count == 0)
    gdk_colormap_real_destroy (cmap);
}

182 183 184 185 186 187 188 189 190 191 192 193
GdkVisual *
gdk_colormap_get_visual (GdkColormap *colormap)
{
  GdkColormapPrivate *private;

  g_return_val_if_fail (colormap != NULL, NULL);
  
  private = (GdkColormapPrivate *)colormap;

  return private->visual;
}
     
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
#define MIN_SYNC_TIME 2

void
gdk_colormap_sync (GdkColormap *colormap,
		   gboolean     force)
{
  time_t current_time;
  GdkColormapPrivate *private = (GdkColormapPrivate *)colormap;
  XColor *xpalette;
  gint nlookup;
  gint i;
  
  g_return_if_fail (colormap != NULL);

  current_time = time (NULL);
  if (!force && ((current_time - private->last_sync_time) < MIN_SYNC_TIME))
    return;

  private->last_sync_time = current_time;

  nlookup = 0;
  xpalette = g_new (XColor, colormap->size);
  
  for (i = 0; i < colormap->size; i++)
    {
      if (private->info[i].ref_count == 0)
	{
	  xpalette[nlookup].pixel = i;
	  xpalette[nlookup].red = 0;
	  xpalette[nlookup].green = 0;
	  xpalette[nlookup].blue = 0;
	  nlookup++;
	}
    }
  
  XQueryColors (gdk_display, private->xcolormap, xpalette, nlookup);
  
  for (i = 0; i < nlookup; i++)
    {
      gulong pixel = xpalette[i].pixel;
      colormap->colors[pixel].pixel = pixel;
      colormap->colors[pixel].red = xpalette[i].red;
      colormap->colors[pixel].green = xpalette[i].green;
      colormap->colors[pixel].blue = xpalette[i].blue;
    }
  
  g_free (xpalette);
}
		   

Elliot Lee's avatar
Elliot Lee committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
GdkColormap*
gdk_colormap_get_system (void)
{
  static GdkColormap *colormap = NULL;
  GdkColormapPrivate *private;

  if (!colormap)
    {
      private = g_new (GdkColormapPrivate, 1);
      colormap = (GdkColormap*) private;

      private->xdisplay = gdk_display;
      private->xcolormap = DefaultColormap (gdk_display, gdk_screen);
      private->visual = gdk_visual_get_system ();
      private->private_val = FALSE;
      private->ref_count = 1;

261 262 263 264 265
      private->hash = NULL;
      private->last_sync_time = 0;
      private->info = NULL;

      colormap->colors = NULL;
266 267
      colormap->size = private->visual->colormap_size;

268 269
      if ((private->visual->type == GDK_VISUAL_GRAYSCALE) ||
	  (private->visual->type == GDK_VISUAL_PSEUDO_COLOR))
Elliot Lee's avatar
Elliot Lee committed
270
	{
271 272
	  private->info = g_new0 (GdkColorInfo, colormap->size);
	  colormap->colors = g_new (GdkColor, colormap->size);
273
	  
274 275
	  private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
					    (GCompareFunc) gdk_color_equal);
276

277
	  gdk_colormap_sync (colormap, TRUE);
Elliot Lee's avatar
Elliot Lee committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	}

      gdk_colormap_add (colormap);
    }

  return colormap;
}

gint
gdk_colormap_get_system_size (void)
{
  return DisplayCells (gdk_display, gdk_screen);
}

void
gdk_colormap_change (GdkColormap *colormap,
		     gint         ncolors)
{
  GdkColormapPrivate *private;
  GdkVisual *visual;
298
  XColor *palette;
Elliot Lee's avatar
Elliot Lee committed
299 300 301 302 303 304 305
  gint shift;
  int max_colors;
  int size;
  int i;

  g_return_if_fail (colormap != NULL);

306 307
  palette = g_new (XColor, ncolors);

Elliot Lee's avatar
Elliot Lee committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
  private = (GdkColormapPrivate*) colormap;
  switch (private->visual->type)
    {
    case GDK_VISUAL_GRAYSCALE:
    case GDK_VISUAL_PSEUDO_COLOR:
      for (i = 0; i < ncolors; i++)
	{
	  palette[i].pixel = colormap->colors[i].pixel;
	  palette[i].red = colormap->colors[i].red;
	  palette[i].green = colormap->colors[i].green;
	  palette[i].blue = colormap->colors[i].blue;
	  palette[i].flags = DoRed | DoGreen | DoBlue;
	}

      XStoreColors (private->xdisplay, private->xcolormap, palette, ncolors);
      break;

    case GDK_VISUAL_DIRECT_COLOR:
      visual = private->visual;

      shift = visual->red_shift;
      max_colors = 1 << visual->red_prec;
      size = (ncolors < max_colors) ? (ncolors) : (max_colors);

      for (i = 0; i < size; i++)
	{
	  palette[i].pixel = i << shift;
	  palette[i].red = colormap->colors[i].red;
	  palette[i].flags = DoRed;
	}

      XStoreColors (private->xdisplay, private->xcolormap, palette, size);

      shift = visual->green_shift;
      max_colors = 1 << visual->green_prec;
      size = (ncolors < max_colors) ? (ncolors) : (max_colors);

      for (i = 0; i < size; i++)
	{
	  palette[i].pixel = i << shift;
	  palette[i].green = colormap->colors[i].green;
	  palette[i].flags = DoGreen;
	}

      XStoreColors (private->xdisplay, private->xcolormap, palette, size);

      shift = visual->blue_shift;
      max_colors = 1 << visual->blue_prec;
      size = (ncolors < max_colors) ? (ncolors) : (max_colors);

      for (i = 0; i < size; i++)
	{
	  palette[i].pixel = i << shift;
	  palette[i].blue = colormap->colors[i].blue;
	  palette[i].flags = DoBlue;
	}

      XStoreColors (private->xdisplay, private->xcolormap, palette, size);
      break;

    default:
      break;
    }
371 372

  g_free (palette);
Elliot Lee's avatar
Elliot Lee committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
}

void
gdk_colors_store (GdkColormap   *colormap,
		  GdkColor      *colors,
		  gint           ncolors)
{
  gint i;

  for (i = 0; i < ncolors; i++)
    {
      colormap->colors[i].pixel = colors[i].pixel;
      colormap->colors[i].red = colors[i].red;
      colormap->colors[i].green = colors[i].green;
      colormap->colors[i].blue = colors[i].blue;
    }

  gdk_colormap_change (colormap, ncolors);
}

393
gboolean
Elliot Lee's avatar
Elliot Lee committed
394 395 396 397 398 399 400 401 402
gdk_colors_alloc (GdkColormap   *colormap,
		  gint           contiguous,
		  gulong        *planes,
		  gint           nplanes,
		  gulong        *pixels,
		  gint           npixels)
{
  GdkColormapPrivate *private;
  gint return_val;
403
  gint i;
Elliot Lee's avatar
Elliot Lee committed
404 405 406 407 408 409 410 411

  g_return_val_if_fail (colormap != NULL, 0);

  private = (GdkColormapPrivate*) colormap;

  return_val = XAllocColorCells (private->xdisplay, private->xcolormap,
				 contiguous, planes, nplanes, pixels, npixels);

412 413 414 415 416 417 418 419 420
  if (return_val)
    {
      for (i=0; i<npixels; i++)
	{
	  private->info[pixels[i]].ref_count++;
	  private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE;
	}
    }

Elliot Lee's avatar
Elliot Lee committed
421 422 423
  return return_val;
}

424 425 426
/* This is almost identical to gdk_colormap_free_colors.
 * Keep them in sync!
 */
Elliot Lee's avatar
Elliot Lee committed
427 428
void
gdk_colors_free (GdkColormap *colormap,
429 430
		 gulong      *in_pixels,
		 gint         in_npixels,
Elliot Lee's avatar
Elliot Lee committed
431 432 433
		 gulong       planes)
{
  GdkColormapPrivate *private;
434 435 436
  gulong *pixels;
  gint npixels = 0;
  gint i;
Elliot Lee's avatar
Elliot Lee committed
437 438

  g_return_if_fail (colormap != NULL);
439
  g_return_if_fail (in_pixels != NULL);
Elliot Lee's avatar
Elliot Lee committed
440 441 442

  private = (GdkColormapPrivate*) colormap;

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
  if ((private->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
      (private->visual->type != GDK_VISUAL_GRAYSCALE))
    return;
  
  pixels = g_new (gulong, in_npixels);

  for (i=0; i<in_npixels; i++)
    {
      gulong pixel = in_pixels[i];
      
      if (private->info[pixel].ref_count)
	{
	  private->info[pixel].ref_count--;

	  if (private->info[pixel].ref_count == 0)
	    {
	      pixels[npixels++] = pixel;
	      if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
		g_hash_table_remove (private->hash, &colormap->colors[in_pixels[i]]);
	      private->info[pixel].flags = 0;
	    }
	}
    }

  if (npixels)
    XFreeColors (private->xdisplay, private->xcolormap,
		 pixels, npixels, planes);
  g_free (pixels);
Elliot Lee's avatar
Elliot Lee committed
471 472
}

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
/*
 *--------------------------------------------------------------
 * gdk_color_copy
 *
 *   Copy a color structure into new storage.
 *
 * Arguments:
 *   "color" is the color struct to copy.
 *
 * Results:
 *   A new color structure.  Free it with gdk_color_free.
 *
 *--------------------------------------------------------------
 */

static GMemChunk *color_chunk;

GdkColor*
gdk_color_copy (GdkColor *color)
{
  GdkColor *new_color;
  
  g_return_val_if_fail (color != NULL, NULL);

  if (color_chunk == NULL)
    color_chunk = g_mem_chunk_new ("colors",
				   sizeof (GdkColor),
				   4096,
				   G_ALLOC_AND_FREE);

  new_color = g_chunk_new (GdkColor, color_chunk);
  *new_color = *color;
  return new_color;
}

/*
 *--------------------------------------------------------------
 * gdk_color_free
 *
 *   Free a color structure obtained from gdk_color_copy.  Do not use
 *   with other color structures.
 *
 * Arguments:
 *   "color" is the color struct to free.
 *
 *-------------------------------------------------------------- */

void
gdk_color_free (GdkColor *color)
{
  g_assert (color_chunk != NULL);
  g_return_if_fail (color != NULL);

  g_mem_chunk_free (color_chunk, color);
}

Elliot Lee's avatar
Elliot Lee committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
gint
gdk_color_white (GdkColormap *colormap,
		 GdkColor    *color)
{
  gint return_val;

  g_return_val_if_fail (colormap != NULL, FALSE);

  if (color)
    {
      color->pixel = WhitePixel (gdk_display, gdk_screen);
      color->red = 65535;
      color->green = 65535;
      color->blue = 65535;

      return_val = gdk_color_alloc (colormap, color);
    }
  else
    return_val = FALSE;

  return return_val;
}

gint
gdk_color_black (GdkColormap *colormap,
		 GdkColor    *color)
{
  gint return_val;

  g_return_val_if_fail (colormap != NULL, FALSE);

  if (color)
    {
      color->pixel = BlackPixel (gdk_display, gdk_screen);
      color->red = 0;
      color->green = 0;
      color->blue = 0;

      return_val = gdk_color_alloc (colormap, color);
    }
  else
    return_val = FALSE;

  return return_val;
}

575
gboolean
Elliot Lee's avatar
Elliot Lee committed
576 577 578 579 580
gdk_color_parse (const gchar *spec,
		 GdkColor *color)
{
  Colormap xcolormap;
  XColor xcolor;
581
  gboolean return_val;
Elliot Lee's avatar
Elliot Lee committed
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600

  g_return_val_if_fail (spec != NULL, FALSE);
  g_return_val_if_fail (color != NULL, FALSE);

  xcolormap = DefaultColormap (gdk_display, gdk_screen);

  if (XParseColor (gdk_display, xcolormap, spec, &xcolor))
    {
      return_val = TRUE;
      color->red = xcolor.red;
      color->green = xcolor.green;
      color->blue = xcolor.blue;
    }
  else
    return_val = FALSE;

  return return_val;
}

601 602 603 604 605 606 607 608 609 610 611
/********************
 * Color allocation *
 ********************/

/* Try to allocate a single color using XAllocColor. If it succeeds,
 * cache the result in our colormap, and store in ret.
 */
static gboolean 
gdk_colormap_alloc1 (GdkColormap *colormap,
		     GdkColor    *color,
		     GdkColor    *ret)
Elliot Lee's avatar
Elliot Lee committed
612 613 614 615
{
  GdkColormapPrivate *private;
  XColor xcolor;

616
  private = (GdkColormapPrivate*) colormap;
Elliot Lee's avatar
Elliot Lee committed
617 618 619 620 621 622 623

  xcolor.red = color->red;
  xcolor.green = color->green;
  xcolor.blue = color->blue;
  xcolor.pixel = color->pixel;
  xcolor.flags = DoRed | DoGreen | DoBlue;

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
  if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
    {
      ret->pixel = xcolor.pixel;
      ret->red = xcolor.red;
      ret->green = xcolor.green;
      ret->blue = xcolor.blue;
      
      if (ret->pixel < colormap->size)
	{
	  if (private->info[ret->pixel].ref_count) /* got a duplicate */
	    {
	      XFreeColors (private->xdisplay, private->xcolormap,
			   &ret->pixel, 1, 0);
	    }
	  else
	    {
	      colormap->colors[ret->pixel] = *color;
	      private->info[ret->pixel].ref_count = 1;

	      g_hash_table_insert (private->hash,
				   &colormap->colors[ret->pixel],
				   &colormap->colors[ret->pixel]);
	    }
	}
      return TRUE;
    }
  else
    {
      return FALSE;
    }
}

static gint
gdk_colormap_alloc_colors_writeable (GdkColormap *colormap,
				     GdkColor    *colors,
				     gint         ncolors,
				     gboolean     writeable,
				     gboolean     best_match,
				     gboolean    *success)
{
  GdkColormapPrivate *private;
  gulong *pixels;
  Status status;
  gint i, index;

Elliot Lee's avatar
Elliot Lee committed
669 670
  private = (GdkColormapPrivate*) colormap;

671
  if (private->private_val)
Elliot Lee's avatar
Elliot Lee committed
672
    {
673 674
      index = 0;
      for (i=0; i<ncolors; i++)
Elliot Lee's avatar
Elliot Lee committed
675
	{
676 677 678 679
	  while ((index < colormap->size) && (private->info[index].ref_count != 0))
	    index++;
	  
	  if (index < colormap->size)
Elliot Lee's avatar
Elliot Lee committed
680
	    {
681 682 683 684
	      colors[i].pixel = index;
	      success[i] = TRUE;
	      private->info[index].ref_count++;
	      private->info[i].flags |= GDK_COLOR_WRITEABLE;
Elliot Lee's avatar
Elliot Lee committed
685 686
	    }
	  else
687 688 689 690 691 692 693 694 695 696 697 698 699 700
	    break;
	}
      return i;
    }
  else
    {
      pixels = g_new (gulong, ncolors);
      /* Allocation of a writeable color cells */
      
      status =  XAllocColorCells (private->xdisplay, private->xcolormap,
				  FALSE, NULL, 0, pixels, ncolors);
      if (status)
	{
	  for (i=0; i<ncolors; i++)
Elliot Lee's avatar
Elliot Lee committed
701
	    {
702 703 704 705 706 707 708 709 710 711 712
	      colors[i].pixel = pixels[i];
	      private->info[pixels[i]].ref_count++;
	      private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE;
	    }
	}
      
      g_free (pixels);

      return status ? ncolors : 0; 
    }
}
Elliot Lee's avatar
Elliot Lee committed
713

714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
static gint
gdk_colormap_alloc_colors_private (GdkColormap *colormap,
				   GdkColor    *colors,
				   gint         ncolors,
				   gboolean     writeable,
				   gboolean     best_match,
				   gboolean    *success)
{
  GdkColormapPrivate *private;
  gint i, index;
  XColor *store = g_new (XColor, ncolors);
  gint nstore = 0;
  gint nremaining = 0;
  
  private = (GdkColormapPrivate*) colormap;
  index = -1;

  /* First, store the colors we have room for */

  index = 0;
  for (i=0; i<ncolors; i++)
    {
      if (!success[i])
	{
	  while ((index < colormap->size) && (private->info[index].ref_count != 0))
	    index++;

	  if (index < colormap->size)
	    {
	      store[nstore].red = colors[i].red;
	      store[nstore].blue = colors[i].blue;
	      store[nstore].green = colors[i].green;
	      store[nstore].pixel = index;
	      nstore++;

	      success[i] = TRUE;

	      colors[i].pixel = index;
	      private->info[index].ref_count++;
Elliot Lee's avatar
Elliot Lee committed
753
	    }
754 755
	  else
	    nremaining++;
Elliot Lee's avatar
Elliot Lee committed
756
	}
757 758 759 760 761 762 763 764 765 766 767 768 769 770
    }
  
  XStoreColors (private->xdisplay, private->xcolormap, store, nstore);
  g_free (store);

  if (nremaining > 0 && best_match)
    {
      /* Get best matches for remaining colors */

      gchar *available = g_new (gchar, colormap->size);
      for (i = 0; i < colormap->size; i++)
	available[i] = TRUE;

      for (i=0; i<ncolors; i++)
Elliot Lee's avatar
Elliot Lee committed
771
	{
772
	  if (!success[i])
Elliot Lee's avatar
Elliot Lee committed
773
	    {
774 775 776 777
	      index = gdk_colormap_match_color (colormap, 
						&colors[i], 
						available);
	      if (index != -1)
Elliot Lee's avatar
Elliot Lee committed
778
		{
779 780
		  colors[i] = colormap->colors[index];
		  private->info[index].ref_count++;
Elliot Lee's avatar
Elliot Lee committed
781

782 783
		  success[i] = TRUE;
		  nremaining--;
Elliot Lee's avatar
Elliot Lee committed
784
		}
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	    }
	}
      g_free (available);
    }

  return (ncolors - nremaining);
}

static gint
gdk_colormap_alloc_colors_shared (GdkColormap *colormap,
				  GdkColor    *colors,
				  gint         ncolors,
				  gboolean     writeable,
				  gboolean     best_match,
				  gboolean    *success)
{
  GdkColormapPrivate *private;
  gint i, index;
  gint nremaining = 0;
  gint nfailed = 0;

  private = (GdkColormapPrivate*) colormap;
  index = -1;
Elliot Lee's avatar
Elliot Lee committed
808

809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
  for (i=0; i<ncolors; i++)
    {
      if (!success[i])
	{
	  if (gdk_colormap_alloc1 (colormap, &colors[i], &colors[i]))
	    success[i] = TRUE;
	  else
	    nremaining++;
	}
    }


  if (nremaining > 0 && best_match)
    {
      gchar *available = g_new (gchar, colormap->size);
      for (i = 0; i < colormap->size; i++)
	available[i] = ((private->info[i].ref_count == 0) ||
			!(private->info[i].flags && GDK_COLOR_WRITEABLE));
      gdk_colormap_sync (colormap, FALSE);
      
      while (nremaining > 0)
	{
	  for (i=0; i<ncolors; i++)
	    {
	      if (!success[i])
		{
		  index = gdk_colormap_match_color (colormap, &colors[i], available);
Elliot Lee's avatar
Elliot Lee committed
836 837
		  if (index != -1)
		    {
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
		      if (private->info[index].ref_count)
			{
			  private->info[index].ref_count++;
			  colors[i] = colormap->colors[index];
			  success[i] = TRUE;
			  nremaining--;
			}
		      else
			{
			  if (gdk_colormap_alloc1 (colormap, 
						   &colormap->colors[index],
						   &colors[i]))
			    {
			      success[i] = TRUE;
			      nremaining--;
			      break;
			    }
			  else
			    {
			      available[index] = FALSE;
			    }
			}
Elliot Lee's avatar
Elliot Lee committed
860 861 862
		    }
		  else
		    {
863 864 865
		      nfailed++;
		      nremaining--;
		      success[i] = 2; /* flag as permanent failure */
Elliot Lee's avatar
Elliot Lee committed
866 867 868 869
		    }
		}
	    }
	}
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
      g_free (available);
    }

  /* Change back the values we flagged as permanent failures */
  if (nfailed > 0)
    {
      for (i=0; i<ncolors; i++)
	if (success[i] == 2)
	  success[i] = FALSE;
      nremaining = nfailed;
    }
  
  return (ncolors - nremaining);
}

static gint
gdk_colormap_alloc_colors_pseudocolor (GdkColormap *colormap,
				       GdkColor    *colors,
				       gint         ncolors,
				       gboolean     writeable,
				       gboolean     best_match,
				       gboolean    *success)
{
  GdkColormapPrivate *private;
  GdkColor *lookup_color;
  gint i;
  gint nremaining = 0;

  private = (GdkColormapPrivate*) colormap;

  /* Check for an exact match among previously allocated colors */

  for (i=0; i<ncolors; i++)
    {
      if (!success[i])
	{
	  lookup_color = g_hash_table_lookup (private->hash, &colors[i]);
	  if (lookup_color)
	    {
	      private->info[lookup_color->pixel].ref_count++;
	      colors[i].pixel = lookup_color->pixel;
	      success[i] = TRUE;
	    }
	  else
	    nremaining++;
	}
    }

  /* If that failed, we try to allocate a new color, or approxmiate
   * with what we can get if best_match is TRUE.
   */
  if (nremaining > 0)
    {
      if (private->private_val)
	return gdk_colormap_alloc_colors_private (colormap, colors, ncolors, writeable, best_match, success);
      else
	return gdk_colormap_alloc_colors_shared (colormap, colors, ncolors, writeable, best_match, success);
    }
  else
    return 0;
}

gint
gdk_colormap_alloc_colors (GdkColormap *colormap,
			   GdkColor    *colors,
			   gint         ncolors,
			   gboolean     writeable,
			   gboolean     best_match,
			   gboolean    *success)
{
  GdkColormapPrivate *private;
  GdkVisual *visual;
  gint i;
  gint nremaining = 0;
  XColor xcolor;

  g_return_val_if_fail (colormap != NULL, FALSE);
  g_return_val_if_fail (colors != NULL, FALSE);

  private = (GdkColormapPrivate*) colormap;

  for (i=0; i<ncolors; i++)
    {
      success[i] = FALSE;
    }

  switch (private->visual->type)
    {
    case GDK_VISUAL_PSEUDO_COLOR:
    case GDK_VISUAL_GRAYSCALE:
      if (writeable)
	return gdk_colormap_alloc_colors_writeable (colormap, colors, ncolors,
						    writeable, best_match, success);
      else
	return gdk_colormap_alloc_colors_pseudocolor (colormap, colors, ncolors,
						    writeable, best_match, success);
Elliot Lee's avatar
Elliot Lee committed
966 967 968
      break;

    case GDK_VISUAL_DIRECT_COLOR:
969
    case GDK_VISUAL_TRUE_COLOR:
Elliot Lee's avatar
Elliot Lee committed
970
      visual = private->visual;
971 972 973 974 975 976 977 978

      for (i=0; i<ncolors; i++)
	{
	  colors[i].pixel = (((colors[i].red >> (16 - visual->red_prec)) << visual->red_shift) +
			     ((colors[i].green >> (16 - visual->green_prec)) << visual->green_shift) +
			     ((colors[i].blue >> (16 - visual->blue_prec)) << visual->blue_shift));
	  success[i] = TRUE;
	}
Elliot Lee's avatar
Elliot Lee committed
979 980 981 982
      break;

    case GDK_VISUAL_STATIC_GRAY:
    case GDK_VISUAL_STATIC_COLOR:
983
      for (i=0; i<ncolors; i++)
Elliot Lee's avatar
Elliot Lee committed
984
	{
985 986 987 988 989 990 991 992 993 994 995 996 997
	  xcolor.red = colors[i].red;
	  xcolor.green = colors[i].green;
	  xcolor.blue = colors[i].blue;
	  xcolor.pixel = colors[i].pixel;
	  xcolor.flags = DoRed | DoGreen | DoBlue;

	  if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
	    {
	      colors[i].pixel = xcolor.pixel;
	      success[i] = TRUE;
	    }
	  else
	    nremaining++;
Elliot Lee's avatar
Elliot Lee committed
998 999 1000
	}
      break;
    }
1001 1002
  return nremaining;
}
Elliot Lee's avatar
Elliot Lee committed
1003

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
gboolean
gdk_colormap_alloc_color (GdkColormap *colormap,
			  GdkColor    *color,
			  gboolean     writeable,
			  gboolean     best_match)
{
  gboolean success;

  gdk_colormap_alloc_colors (colormap, color, 1, writeable, best_match,
			     &success);

  return success;
}

/* This is almost identical to gdk_colors_free.
 * Keep them in sync!
 */
void
gdk_colormap_free_colors (GdkColormap *colormap,
			  GdkColor    *colors,
			  gint         ncolors)
{
  GdkColormapPrivate *private;
  gulong *pixels;
  gint npixels = 0;
  gint i;

  g_return_if_fail (colormap != NULL);
  g_return_if_fail (colors != NULL);

  private = (GdkColormapPrivate*) colormap;

  if ((private->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
      (private->visual->type != GDK_VISUAL_GRAYSCALE))
    return;

  pixels = g_new (gulong, ncolors);

  for (i=0; i<ncolors; i++)
    {
      gulong pixel = colors[i].pixel;
      
      if (private->info[pixel].ref_count)
	{
	  private->info[pixel].ref_count--;

	  if (private->info[pixel].ref_count == 0)
	    {
	      pixels[npixels++] = pixel;
	      if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
		g_hash_table_remove (private->hash, &colors[i]);
	      private->info[pixel].flags = 0;
	    }
	}
    }

  if (npixels)
    XFreeColors (private->xdisplay, private->xcolormap,
		 pixels, npixels, 0);

  g_free (pixels);
}

gboolean
gdk_color_alloc (GdkColormap *colormap,
		 GdkColor    *color)
{
  gboolean success;

  gdk_colormap_alloc_colors (colormap, color, 1, FALSE, TRUE, &success);

  return success;
Elliot Lee's avatar
Elliot Lee committed
1076 1077
}

1078
gboolean
Elliot Lee's avatar
Elliot Lee committed
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
gdk_color_change (GdkColormap *colormap,
		  GdkColor    *color)
{
  GdkColormapPrivate *private;
  XColor xcolor;

  g_return_val_if_fail (colormap != NULL, FALSE);
  g_return_val_if_fail (color != NULL, FALSE);

  xcolor.pixel = color->pixel;
  xcolor.red = color->red;
  xcolor.green = color->green;
  xcolor.blue = color->blue;
  xcolor.flags = DoRed | DoGreen | DoBlue;

  private = (GdkColormapPrivate*) colormap;
  XStoreColor (private->xdisplay, private->xcolormap, &xcolor);

  return TRUE;
}

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
guint
gdk_color_hash (const GdkColor *colora,
		const GdkColor *colorb)
{
  return ((colora->red) +
	  (colora->green << 11) +
	  (colora->blue << 22) +
	  (colora->blue >> 6));
}

Elliot Lee's avatar
Elliot Lee committed
1110
gint
1111 1112
gdk_color_equal (const GdkColor *colora,
		 const GdkColor *colorb)
Elliot Lee's avatar
Elliot Lee committed
1113 1114 1115 1116 1117 1118 1119 1120 1121
{
  g_return_val_if_fail (colora != NULL, FALSE);
  g_return_val_if_fail (colorb != NULL, FALSE);

  return ((colora->red == colorb->red) &&
	  (colora->green == colorb->green) &&
	  (colora->blue == colorb->blue));
}

1122 1123 1124
/* XXX: Do not use this function until it is fixed. An X Colormap
 *      is useless unless we also have the visual.
 */
Elliot Lee's avatar
Elliot Lee committed
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
GdkColormap*
gdkx_colormap_get (Colormap xcolormap)
{
  GdkColormap *colormap;
  GdkColormapPrivate *private;

  colormap = gdk_colormap_lookup (xcolormap);
  if (colormap)
    return colormap;

  if (xcolormap == DefaultColormap (gdk_display, gdk_screen))
    return gdk_colormap_get_system ();

  private = g_new (GdkColormapPrivate, 1);
  colormap = (GdkColormap*) private;

  private->xdisplay = gdk_display;
  private->xcolormap = xcolormap;
  private->visual = NULL;
  private->private_val = TRUE;

1146 1147 1148 1149 1150 1151
  /* To do the following safely, we would have to have some way of finding
   * out what the size or visual of the given colormap is. It seems
   * X doesn't allow this
   */

#if 0
Elliot Lee's avatar
Elliot Lee committed
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
  for (i = 0; i < 256; i++)
    {
      xpalette[i].pixel = i;
      xpalette[i].red = 0;
      xpalette[i].green = 0;
      xpalette[i].blue = 0;
    }

  XQueryColors (gdk_display, private->xcolormap, xpalette, 256);

  for (i = 0; i < 256; i++)
    {
      colormap->colors[i].pixel = xpalette[i].pixel;
      colormap->colors[i].red = xpalette[i].red;
      colormap->colors[i].green = xpalette[i].green;
      colormap->colors[i].blue = xpalette[i].blue;
    }
1169 1170 1171 1172
#endif

  colormap->colors = NULL;
  colormap->size = 0;
Elliot Lee's avatar
Elliot Lee committed
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196

  gdk_colormap_add (colormap);

  return colormap;
}


static gint
gdk_colormap_match_color (GdkColormap *cmap,
			  GdkColor    *color,
			  const gchar *available)
{
  GdkColor *colors;
  guint sum, max;
  gint rdiff, gdiff, bdiff;
  gint i, index;

  g_return_val_if_fail (cmap != NULL, 0);
  g_return_val_if_fail (color != NULL, 0);

  colors = cmap->colors;
  max = 3 * (65536);
  index = -1;

1197
  for (i = 0; i < cmap->size; i++)
Elliot Lee's avatar
Elliot Lee committed
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
    {
      if ((!available) || (available && available[i]))
	{
	  rdiff = (color->red - colors[i].red);
	  gdiff = (color->green - colors[i].green);
	  bdiff = (color->blue - colors[i].blue);

	  sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);

	  if (sum < max)
	    {
	      index = i;
	      max = sum;
	    }
	}
    }

  return index;
}


GdkColormap*
gdk_colormap_lookup (Colormap xcolormap)
{
  GdkColormap *cmap;

  if (!colormap_hash)
    return NULL;

  cmap = g_hash_table_lookup (colormap_hash, &xcolormap);
  return cmap;
}

static void
gdk_colormap_add (GdkColormap *cmap)
{
  GdkColormapPrivate *private;

  if (!colormap_hash)
    colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
				      (GCompareFunc) gdk_colormap_cmp);

  private = (GdkColormapPrivate*) cmap;

  g_hash_table_insert (colormap_hash, &private->xcolormap, cmap);
}

static void
gdk_colormap_remove (GdkColormap *cmap)
{
  GdkColormapPrivate *private;

  if (!colormap_hash)
    colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
				      (GCompareFunc) gdk_colormap_cmp);

  private = (GdkColormapPrivate*) cmap;

  g_hash_table_remove (colormap_hash, &private->xcolormap);
}

static guint
gdk_colormap_hash (Colormap *cmap)
{
  return *cmap;
}

static gint
gdk_colormap_cmp (Colormap *a,
		  Colormap *b)
{
  return (*a == *b);
}